We have application where we ask user to define time when they want to publish some content. In our application, user will select that content should publish X(0-5) day before target date and y time (0-23).
So if user create a content with publish date = "09/21/2021" and publish time as 3 it means this content needs to publish at 3 AM on 09/21/2021.
In our Mongo DB we need to store the calculated date as "2021-09-21T07:00:00" So it can be pulled by our Cron Job.
We tried will following without any success
var promotedDate = "2021-08-19 04:00:00" var fmt = "YYYY-MM-DD hh:mm:ss"; var m = moment.utc(promotedDate, fmt); console.log('promoted date ', tempDate) console.log(m.local().format(fmt)); console.log(m.utc().format(fmt));
But it gives 2021-08-19 12:00:00
Any help?
You can use moment.toISOString()
. That will give you UTC regardless of the timezone of the moment
in question thus: 2013-02-04T22:44:30.652Z
.
Since that comes with milliseconds and a Z
for Zulu time, we need to strip off the last 5 characters with string.slice()
:
const promotedDate = "2021-08-19 04:00:00";
const m = moment.utc(promotedDate, "YYYY-MM-DD hh:mm:ss");
const iso8601 = m.toISOString()
.slice(0,-5); // removes milliseconds and `Z` for Zulu
console.log( `promoted: ${promotedDate}` );
console.log( `iso8601: ${iso8601} );
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.