Convert user define time to UTC using moment - node.js

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} );

Related

Reactjs Timezone convertion

I am connected mssql database and get some informations includes Date_Time.
Time is coming like 2021-01-30T15:08:25.357Z. I want to convert it to dd-mm-yy hh:mm:ss format.
So, it should be 30-01-2021 15:08:25.
I used this method but it is not exactly that I want.
var d1 = new Date(datey).toLocaleDateString("tr")
var newTime=d1+" "+
new Date(datey).getUTCHours()+":"+
new Date(datey).getUTCMinutes()+":"+
new Date(datey).getUTCSeconds()
// it returns 30/01/2021 15:8:25
Maybe, In there, I want to see time fomat with 0 such as 15:08. When hour 2 a.m it just 2:0 but I want to see it 02:00.
How should I do , is there any idea?
I would suggest using a date/time library such as moment.js, this will make date manipulation much easier, parsing and formatting your date is then very simple:
const input= "2021-01-30T15:08:25.357Z";
console.log("Input date:", input);
// To convert the date to local before displaying, we can use moment().format()
const formattedDateLocal = moment(input).format("DD-MM-YY HH:mm:ss");
console.log("Formatted date (Local Time):", formattedDateLocal );
// To display the UTC date, we can use moment.utc().format()
const formattedDateUTC = moment.utc(input).format("DD-MM-YY HH:mm:ss");
console.log("Formatted date (UTC):", formattedDateUTC );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Wrong date format with toLocaleString on server side

Using this on a server side (a Firebase function, nodejs )
var d=new Date();
var date = d.toLocaleString('default', { month: 'long'}) + ", " + d.getFullYear();
I am expecting to get March, 2020 , since this is what i get on the JS client side, but instead when run on the server side i get - M03, 2020 .
I tried the following code:
const date = require('date-and-time');
var d = new Date();
var myDate = d.toLocaleString('default', { month: 'long'}) + ", " + d.getFullYear();
console.log(myDate);
Output:
March, 2020
Now, if you are trying to store a Javascript Date object into Firebase, this object will be stored as a Timestamp object. I think this would explain the reason why you are noticing some discrepancies in the format.
Optionally, you can convert the Date object to String and store it in Firebase as such. This is more human-readable than ISO 8601 and Timestamp - 0001-01-01T00:00:00Z
You can read more about this in the following documentation about Timestamp and Data types. Moreover, there exists a Timestamp built-in method toDate() that returns a new Date corresponding to a certain timestamp, if conversion is needed.

How to set timezone with moment?

i am using moment for getting server time .
moment.tz.setDefault("Asia/Kolkata");
var now = new Date();
var _p_date = moment.tz(now, zone).format();
time when inserting _p_date = 2016-01-05T18:32:00+05:30
But in database date variable is type of DATETIME. and time is saved as 2016-01-05 18:32:00.
and after that when i comparing with this to get time_ago funcionality. providing me wrong estimation.
using time ago = moment("2016-01-05T18:32:00.000Z").fromNow(); // is showing In 5 hours
Since your initial timezone is lost you have to create moment.tz object with selected timezone. Try this plunker
var date = moment.tz(moment("2016-01-05T18:32:00.000Z", "YYYY-MM-DDTHH:mm")
.format('YYYY-MM-DD HH:mm'), 'Asia/Kolkata');
console.log(date.fromNow());

How to determine two date is same day at local time in nodejs?

Here are two date, 2015-11-10 09:00:00+08:00 and 2015-11-10 01:00:00+08:00 is same day in China. But they are different day in UTC.
So... How can I determine two UTC date is same day at different timezone in node.js?
Sorry for my ambiguous description and poor English.
I have two strings: start_time and end_time. And clint upload two other strings: timezone_str, utc_time_str.
I want to determine the client time is between start and end time.
e.g
start_time: 2015-11-10 00:00:00
end_time: 2015-11-11 00:00:00
utc_time_str: 2015-11-10 02:00:00Z
timezone_str: America/New_York
except client_time.isBetween(start_time, end_time) == false.
Thanks to #Matt Johnson's answer, moment-timezone can solve my problem.
client_time = moment(utc_time_str).tz(timezone_str);
start = moment.tz(start_time, timezone_str);
end = moment.tz(end, timezone_str);
client_time.isBetween(start, end) === false
The most reliable way would be to use the parseZone function in moment.js to retain the provided offset. Then use the isSame function with the 'day' argument, which tests for same year, month, and day.
var m1 = moment.parseZone("2015-11-10 09:00:00+08:00");
var m2 = moment.parseZone("2015-11-10 01:00:00+08:00");
if (m1.isSame(m2, 'day')) {
// ...
}
With this approach, it does not matter what offset was given, nor does it matter what the time zone is on the computer where the code is running. It simply compares the two dates as provided.
calculate local "hours" like this
var d1 = new Date('2015-11-10 09:00:00+08:00')
var d2 = new Date('2015-11-10 01:00:00+08:00')
var h1 = d1.getHours() - d1.getTimezoneOffset() / 60
var h2 = d2.getHours() - d2.getTimezoneOffset() / 60
var date1 = d1.getDate() + (h1 >= 24? 1: 0)
var date2 = d2.getDate() + (h2 >= 24? 1: 0)
just compare date1 and date2
Solution without MomentJS
If you have the timezone string of the user that is in accordance with the tz database, such as "America/New_York" — which you can get client side for example by doing: const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
Then you could, for each date, create a new date with the timezone offset added to the original date, for example by doing const dateInTimezone = new Date(new Date(myDate).toLocaleString("en-US", { timeZone: timezone }));
And then reset the time values to exactly midnight of the same day by doing dateInTimezone.setHours(0, 0, 0, 0);
Then you could compare the milliseconds since Epoch values (date we start counting UTC from) of the two dates to see whether they start in the same day or not: const isSameDay = startOfDate1.valueOf() === startOfDate2.valueOf();
Ideally, your starting dates would always be in UTC, since both your server and client could be in all sorts of timezones. So before storing the data, convert the original date to its UTC version in milliseconds since Epoch, by doing myOrignalDate.valueOf() or myOrignalDate.getTime() — this way, you will ensure that timezone offsets added later will be true.
Example
function isSameDayInTimezone(d1, d2, timezone) {
const start_of_d1 = new Date(new Date(d1).toLocaleString("en-US", { timeZone: timezone })).setHours(0, 0, 0, 0).valueOf();
const start_of_d2 = new Date(new Date(d2).toLocaleString("en-US", { timeZone: timezone })).setHours(0, 0, 0, 0).valueOf();
return start_of_d1 === start_of_d2;
}
/* Initialize dates. Note that this will be the UTC time as milliseconds since
Epoch of the date in whichever timezone it was originally captured in. If I
capture it in my timezone, it would currently be UTC/GMT+3, hence if I were
to want to perform server-side actions taking into account my timezone, I'd
also need to capture my tz database timezone string first, to then be able
to, server-side, add my timezone offset to the captured UTC time. */
const date1 = new Date('August 31, 2022 20:16:00').valueOf();
const date2 = new Date('August 31, 2022 23:50:00').valueOf();
const date3 = new Date('August 31, 2022 00:00:00').valueOf();
const date4 = new Date('August 30, 2022 15:00:00').valueOf();
const fixedTimezone = "Europe/Tallinn";
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
/* With fixed timezone */
console.log(isSameDayInTimezone(date1, date2, fixedTimezone));
console.log(isSameDayInTimezone(date1, date3, fixedTimezone));
console.log(isSameDayInTimezone(date1, date4, fixedTimezone));
/* With local timezone */
console.log(isSameDayInTimezone(date1, date2, localTimezone));
console.log(isSameDayInTimezone(date1, date3, localTimezone));
console.log(isSameDayInTimezone(date1, date4, localTimezone));
Voila, no MomentJS needed.

MomentJS getting JavaScript Date in UTC

I am not able to get the JavaScript Date string for MongoDB record via the following. It keeps using my local time.
var utc = moment.utc().valueOf();
console.log(moment.utc(utc).toDate());
Output:
Tue Nov 11 2014 14:42:51 GMT-0500 (EST)
I need it to be in UTC, so I can stick this timestamp in Mongo so type would be Date.
How can I do that?
A timestamp is a point in time. Typically this can be represented by a number of milliseconds past an epoc (the Unix Epoc of Jan 1 1970 12AM UTC). The format of that point in time depends on the time zone. While it is the same point in time, the "hours value" is not the same among time zones and one must take into account the offset from the UTC.
Here's some code to illustrate. A point is time is captured in three different ways.
var moment = require( 'moment' );
var localDate = new Date();
var localMoment = moment();
var utcMoment = moment.utc();
var utcDate = new Date( utcMoment.format() );
//These are all the same
console.log( 'localData unix = ' + localDate.valueOf() );
console.log( 'localMoment unix = ' + localMoment.valueOf() );
console.log( 'utcMoment unix = ' + utcMoment.valueOf() );
//These formats are different
console.log( 'localDate = ' + localDate );
console.log( 'localMoment string = ' + localMoment.format() );
console.log( 'utcMoment string = ' + utcMoment.format() );
console.log( 'utcDate = ' + utcDate );
//One to show conversion
console.log( 'localDate as UTC format = ' + moment.utc( localDate ).format() );
console.log( 'localDate as UTC unix = ' + moment.utc( localDate ).valueOf() );
Which outputs this:
localData unix = 1415806206570
localMoment unix = 1415806206570
utcMoment unix = 1415806206570
localDate = Wed Nov 12 2014 10:30:06 GMT-0500 (EST)
localMoment string = 2014-11-12T10:30:06-05:00
utcMoment string = 2014-11-12T15:30:06+00:00
utcDate = Wed Nov 12 2014 10:30:06 GMT-0500 (EST)
localDate as UTC format = 2014-11-12T15:30:06+00:00
localDate as UTC unix = 1415806206570
In terms of milliseconds, each are the same. It is the exact same point in time (though in some runs, the later millisecond is one higher).
As far as format, each can be represented in a particular timezone. And the formatting of that timezone'd string looks different, for the exact same point in time!
Are you going to compare these time values? Just convert to milliseconds. One value of milliseconds is always less than, equal to or greater than another millisecond value.
Do you want to compare specific 'hour' or 'day' values and worried they "came from" different timezones? Convert to UTC first using moment.utc( existingDate ), and then do operations. Examples of those conversions, when coming out of the DB, are the last console.log calls in the example.
Calling toDate will create a copy (the documentation is down-right wrong about it not being a copy), of the underlying JS Date object. JS Date object is stored in UTC and will always print to eastern time. Without getting into whether .utc() modifies the underlying object that moment wraps use the code below.
You don't need moment for this.
new Date().getTime()
This works, because JS Date at its core is in UTC from the Unix Epoch. It's extraordinarily confusing and I believe a big flaw in the interface to mix local and UTC times like this with no descriptions in the methods.
This will give you the UTC timezone DateTime.
var convertedUtcDateTime = moment.utc(dateTimeToBeConverted);
When you call .utc(), all you're doing is setting a flag in the Moment object that says "this must format as UTC." You can see this change if you inspect the object's properties.
As another answer mentioned, the point in time remains the same. This ensures calculations etc. can be carried out without worrying about the time zone.
A JavaScript Date also stores timezone information, and similarly will format the value it represents as local or UTC depending on what method you call.
Option 1
Use the formatting methods as they were intended, and make the last thing you do before sending the value to the server a format into a UTC string.
For Moment, that's simply calling .toISOString() on any date local or otherwise, or the .format() method after calling .utc() if you want to specify a different format.
For JS dates, that's also a .toISOString() method.
If for some reason you don't have the ability to keep the dates in local time, or can't transform to a string for the data send, you have (at least) two further options:
Option 2
Use the JS Date constructor to manually create a datetime in the local timezone, pulling the components from your UTC Moment object.
let utc = moment().utc();
let date = new Date(utc.year(), utc.month(), utc.date(), utc.hour(), utc.minute(), utc.second(), utc.millisecond());
Alternatively, get the current datetime and then set the hours and minutes as appropriate for the timezone change.
You'll end up with a date representation of the UTC time marked as local time. This is not the correct time, as you're now offset by the time zone, but it might solve a problem in a pinch.
Option 3
Similar to option 2, this is another way to create an offset Date where UTC is marked as the local time zone.
Export the ISO string from either Moment or JS, strip off the Zulu flag at the end, and use the string constructor for Date to pull it back in.
new Date(new Date().toISOString().slice(0,-1));
Again, this is not actually the correct time and calculations against other datetimes may be incorrect. Your best option is still to send up a UTC ISO string to the server when you can.
Or simply:
Date.now
From MDN documentation:
The Date.now() method returns the number of milliseconds elapsed since January 1, 1970
Available since ECMAScript 5.1
It's the same as was mentioned above (new Date().getTime()), but more shortcutted version.

Resources