简体   繁体   中英

How to convert date and time format to timestamp in React Native using moment?

I need to convert date and time in format like: moment(new Date()).format("YYYY-MM-DD hh:mm:ss") to timestamp ? How can I do that ?

Try this:

let date = new Date(Date.UTC(year, month - 1, day, hour, minute, second));
let timeStamp =  date.getTime() / 1000;

this is possible with both Date and Moment , try this

const moment = require('moment')

const time = moment() // moment(new Date()).format("YYYY-MM-DD hh:mm:ss")

const timestamp = {
    unix: time.unix(),
    utc: time.format("YYYY-MM-DD HH:mm:ss")
} 

output { unix: 1605126864, utc: '2020-11-11 17:34:24' }

if you want to work with moment-timezone

const moment = require('moment-timezone')

const time = moment().tz('America/Sao_Paulo')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM