简体   繁体   中英

convert UTC time to EPOCH seconds in Javascript

2020-08-01 11:53:25.85088067 +0000 UTC m=+154637.818754072

I am receiving time in this format, need help to convert it to 1st August 2020 11:53AM in JavaScript

You can use moment library. Try this:

 let moment = require('moment'); let dateString = "2020-08-01 11:53:25.85088067 +0000 UTC m=+154637.818754072" dateString = dateString.split("+")[0]; let date = new Date(dateString); let formattedDate = moment(date).format('Do MMMM YYYY, h:mm:ss a'); console.log(formattedDate); //1st August 2020, 11:53:25 am

 const dateTimeString = "2020-08-01 11:53:25.85088067 +0000 UTC m=+154637.818754072" const dateTimeArray = dateTimeString.split(' ') const [date, time] = dateTimeArray const dateArray = date.split('-') const timeArray = time.split(':') const [year, month, day] = dateArray const [hour, minute, second] = timeArray const utcDate = new Date(Date.UTC(year, month - 1, day, hour, minute, second)); console.log(utcDate.toUTCString()) console.log(utcDate.getTime() / 1000)

If you want the value in second then:

Math.floor(new Date('2011-03-29 17:06:21 UTC') / 1000); It will give you Unix time in second..( In the above example: 1301418381)

If you want full date object: As per mdn ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC ) use this:

var dateString = "2020-08-01 11:53:25.85088067 +0000 UTC m=+154637.818754072"

dateString.toUTCString()

If I understood the question wrong, please comment or reply, so that I can understand it properly. Thanks

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