How to format the date and time into UTC format? - node.js

I'm working in nodejs.
I have the input has date and time "1/6/2022, 2:49:22 AM".
need to convert the date and time to UTC format "2022-01-05T13:04:22.137Z".
please help me with the solution.
code:
let date =new Date().toLocaleString("en-US", { timeZone: "Pacific/Chatham" });
console.log(date);
how to convert this date and time "1/6/2022, 2:49:22 AM" into UTC format "2022-01-05T13:04:22.137Z".
kindly help me with the solution.

In javascript
var isoDateString = new Date().toISOString();
console.log(isoDateString);
The toUTCString() method returns a date object as a string, according to UTC.
Tip: The Universal Coordinated Time (UTC) is the time set by the World Time Standard.
Note: UTC time is the same as GMT time.
Source

Related

How to create date object and get timezone abbreviation from ISO 8601 date format string in node js

I want to convert 2021-11-05T08:41:37-05:00 string to something as 11-05-2021 8:41am EST with time zone abbreviation at the end as EST CST PST etc
Is there is a way to capture timezone abbreviation from ISO String?
I have tried following option
var d =new Date(Date.parse('2021-11-05T08:41:37-05:00'));
console.log(d.toLocaleString('en-US', { timeZone: 'US/Eastern' }))
which gives me string as 11/5/2021, 9:41:37 AM but does not contain any zone abbreviation at the end.

Always getting wrong time difference using Moment

I have end date time in UTC format and with that time I am doing comparing it with current time on my server. I want to check that time difference should be 5 minutes. But I am always getting huge time difference.
console.log('utc format... ', currentDate, slot.endTimeWithDuration);
// log -> utc format... moment("2019-11-30T10:16:00.002") 2019-11-30T16:40:00Z
console.log('diff.... ', moment(slot.endTimeWithDuration).diff(currentDate,'minutes'))
// diff.... 383
How can I resolve this?
Try this something like :
moment.utc(moment(endTime, "HH:mm:ss").diff(moment(startTime, "HH:mm:ss"))).format("mm")
Or You can also make it more simple like below example:
var startTime = moment("07:27:28 am", 'hh:mm:ss a');
var endTime = moment("08:24:59 pm", 'hh:mm:ss a');
endTime.diff(startTime, 'hours');

moment invalid date - string to moment - hh:mm:ss zzz mm/dd/yyyy (02:10:02 SGT 06/07/2018)

I am using moment-timezone
I have datetime with timezone string like below,
02:10:02 EST 07/06/2018 (America/New_York)
02:10:02 MST 07/06/2018 (America/Denver)
02:10:02 CST 07/06/2018 (America/Chicago)
02:10:02 PST 07/06/2018 (America/Los_Angles)
For above four strings I am able to convert to moment by doing just like
const momentTz = require('moment-timezone');
momentTz('02:10:02 EST 07/06/2018');
02:10:02 IST 07/06/2018 (Asia/Calcutta)
02:10:02 SGT 07/06/2018 (Asia/Singapore)
But When I am doing for singapore timezone string momentTz('02:10:02 SGT 07/06/2018'); It's giving me invalid date error.
Can you please help me with this, I need to convert this kind of datetime with timezone string to moment ?
I have datetime strings in different timezones.
Here is the link to all time zone allowed for moment.js.
https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json
You can see "EST", "MST", "CST", "PST" are all listed but there is NO "SGT". You may use the same timeszone area like "Asia/Kuala_Lumpur" or "SMT" instead.

Moment.js sets dates to 1 day behind

I am using 2.22.1 to format dates.
When i put in a date that comes from a date selector, moment will put the date back one day. For example, when I try to format a date in the following way:
Example 1:
const day = new Date(value).getDate();
const month = new Date(value).getMonth();
const fullYear = new Date(value).getFullYear();
console.log('day', day); // 7
console.log('month', month); // 5
console.log('fullYear', fullYear); //2018
Formatting function:
moment.utc(new Date(month + ' ' + day + ' ' + fullYear), 'MM-DD-YYYY').format('DD-MM-YY')
Output: 06-05-18
Expected: 07-05-18
Example 2:
Input: Thu May 31 2018 00:00:00 GMT+0100 (GMT Summer Time)
Formatting function:
moment.utc(new Date(value)).format('DD-MM-YY')
Output: 30-05-18
Expected: 31-05-18
moment.utc will convert the input to universal time.
You are running new Date(...) with an input that's based on GMT +1.
If you think about this, it makes total sense.
Your output is 30-05-18, because it's 11 PM / 23:00 o'clock on the previous day.
This (how ever) would in fact work:
moment('05-06-2018', 'MM-DD-YYYY').format('DD-MM-YY')
// alternatively (and preferrably) this:
moment.utc('05-06-2018', 'MM-DD-YYYY').format('DD-MM-YY')
and output: "06-05-18"
Because the non utc version does not take a time input in this example.
One of the reasons moment.js exists, is to get rid of Date in your code all together.
(Keep in mind tho, that Date has drastically improved now. DateTimeFormat is a game changer)
Please just read the momentjs documentation on how to properly use moment.
edit:
If you want to process 400000 dates with this, I'd advise using RegExp, .split, .exec, .slice or Date instead.
(I can relate since I wrote a client sided Log parser with javascript generators and Service Workers for a statistical anti-cheat analysis)
I truly recommend playing around with such things to raise your knowledge.
I just ran into this issue and a quick fix I found for the time being processed in "Zulu time" (due to the Z at the end of the string) is to add .LocaleString() after the date variable.
I find that for data consistency, it's easier to store data in UTC time and then convert it to the locale string when displaying the data to the user.
For example, I'm using:
moment.utc(dateVariable.toLocaleString()).format("MM/DD/YYYY")

Parsing dates in node.js: moment.utc() is not returning the correct UTC unix time

I'm trying to parse a date string and return the corresponding unix time to midnight at the start of the date in the UTC zone. However, my node.js keeps returning the unixtime at the start of that date in MY timezone.
What am I doing wrong? This should print 1440633600 to the console but it prints a number corresponding to the unix time in my timezone, not UTC.
var moment = require('moment');
var datestring = "August 27 2015";
var unixDate = new Date(datestring);
var myDate = moment.utc(unixDate);
console.log(myDate.format('X'));
"datestring" will not always contain a "Z" at the end (which indicates UTC) but it might sometimes, so I can't just add a "Z" before parsing. There's got to be something I'm missing here...
TL;DR:
var myDate = moment.utc(datestring, 'MMMM DD YYYY');
The explanation:
var unixDate = new Date(datestring); with datestring set to "August 27 2015" will set unixDate to the timestamp corresponding to midnight on August 27 2015 in your time zone.
Subsequently, var myDate = moment.utc(unixDate) sets myDate to a moment object set to that same time. Since you are sending it a timestamp and not a date, it has no reason to adjust for timezone. Since you generated the timestamp based on the time in your timezone, that's what moment sees.
One fix is to change that line to var myDate = moment.utc(datestring, 'MMMM DD YYYY');. Since moment will be getting a string to process rather than a UNIX timestamp, it will select midnight on August 27 2015 in UTC.

Resources