Convert time to UTC from desire timezone - node.js

I have to convert time to UTC time from various time zone as per requirements.
I'm using momnet with node, and I have coded as below:
const inputTime = "10:00";
const timezone = "Europe/Budapest";
const localTime = moment.tz(inputTime, timezone).utc().format("HH:mm");
console.log(localTime);
I'm getting Invalid date as o/p.
Please help me to solve it.

You also need to supply the format of the input string you are parsing.
const inputTime = "10:00";
const timezone = "Europe/Budapest";
const localTime = moment.tz(inputTime, "HH:mm", timezone).utc().format("HH:mm");
console.log(localTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.38/moment-timezone-with-data-10-year-range.min.js"></script>
Keep in mind that the input is representing the time on the current date only. The conversion may return a different value depending on when you run it.

Related

Importing date without local time transformation

I am getting a date in DD-MMM-YY format, and am using dayjs to convert it to standard date format in nodejs.
However, the returned date is 1 day earlier than the input date.
I was told this is probably due to some difference in the server local time.
I can easily just add a day to the date, but as this will be a global function that works in multiple time zones, I just want to get the date "as is" without any automatic adjustments.
This is my function:
const convertDate = (date,format,zone) => {
dayjs.tz.setDefault(zone);
console.log(date);
console.log(dayjs(date));
console.log(dayjs.utc(date).$d);
console.log(dayjs.tz(date,format,zone).$d);
var newDate = dayjs.tz(date,zone);
//newDate.setDate(newDate.getDate());
return newDate;
}
No matter which methods I use or which zone I set, the date comes out as one day earlier than the input date.
For example, for a date of 01-APR-03 I get:
2003-03-31T21:00:00.000Z
I want the time to just be 2003-04-01T00:00:00.000Z.
Following comments, I have tried the following approach, but the result is the same:
const fixMonthName = (s) => s.replace(/[A-Z]{2}-/, (m) => m.toLowerCase());
const d = dayjs.utc(fixMonthName("22-FEB-02"), "DD-MMM-YY");
console.log(d);
const s = d.toISOString();
console.log(s); //{result:
M {
'$L': 'en',
'$u': true,
'$d': 2002-02-21T22:00:00.000Z,
'$x': {},
'$y': 2002,
'$M': 1,
'$D': 21,
'$W': 4,
'$H': 22,
'$m': 0,
'$s': 0,
'$ms': 0
}}
2002-02-21T22:00:00.000Z
Let's recap the problem:
You have a date-only string value of 01-APR-03 (equivalent to 2003-04-01).
You're then parsing it as timestamp, treating it as if it were 2003-04-01T00:00:00.000 (local time). This is the cause of the logical error.
Then you're looking at a UTC representation (2003-03-31T21:00:00.000Z in your example), and wondering why it's been shifted. (Z means UTC)
Fundamentally, a date and a timestamp are two different concepts. If you conflate them, you will have complications in your code such as the one you described.
A date can be thought of as a half-open range of timestamps (from the start of one day, to just before the start of the next). In other words, logically the following is true:
'2003-04-01' == ['2003-04-01T00:00:00.000', '2003-04-02T00:00:00.000')
If you parse a date-only value to an object that represents a timestamp, you are choosing to assign a point-in-time within that range. Thus, if you pick the very start of the range, you can easily shift into a different day when viewing that from another time zone.
Note that the JavaScript Date object is misnamed. It isn't a date, it's a timestamp.
A day.js object also represents a timestamp, as do most other libraries including Moment, Luxon, date-fns, and many others.
There are a few different solutions to this problem:
You can pick a time in the middle of the range which is less likely to be shifted to a different date when viewed from another time zone. For example, 12:00:00 noon. (Though this isn't perfect, as there are some time zones that go up to UTC+14.)
You can avoid treating a date as a timestamp, by keeping it in an object or string that represents it as a whole date.
Unfortunately, this isn't a concept that has caught on well in JavaScript yet. The language and most libraries do not handle it this way. (One notable exception is js-joda, which has a LocalDate data type.) However, this will eventually be coming to the JavaScript language itself via the Temporal proposal, which adds Temporal.PlainDate.
You can ignore the time portion of a timestamp and only look at the date part, but this only works if you lock all your operations to UTC rather than local time. In other words, treat '2003-04-01' as if it were '2003-04-01T00:00:00.000Z' and never convert it to local time or another time zone.
If you were using just the JavaScript Date object, then you would do:
const d = new Date('2003-04-01T00:00:00.000Z'); // the Z parses as UTC
const s = d.toISOString(); // this always emits UTC
But since you have a custom date format to parse and want to use day.js, you can do something like the following:
Define a function to work around day.js's parsing case sensitivity issue. (You need Apr, not APR.)
const fixMonthName = (s) => s.replace(/[A-Z]{2}-/, (m) => m.toLowerCase());
Parse the input string using day.js's UTC mode
const d = dayjs.utc(fixMonthName('01-APR-03'), 'DD-MMM-YY');
Get the output as a string however you would like, using any of day.js's display functions:
const s = d.toISOString(); // "2003-04-01T00:00:00.000Z"
// or
const s = d.format('YYYY-MM-DD'); // "2003-04-01"
Note that if you need a JavaScript Date object, do not use $d but instead call .toDate(). From there, make sure you are only using the UTC representation of the Date object. Keep in mind that while some environments will emit UTC when logging a Date object to the console (as if you called .toISOString(), other environments will emit the local time equivalent (as if you called .toString().

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>

momentJS: Getting wrong timestamp from date in a different timezone

There is one time input i.e. start_time
I am trying to get timestamp in milliseconds for these inputs
let start_time = "17:05:00";
var start_date_moment = moment(start_time, "HH:mm:ss");
console.log(start_timestamp);
output is -> moment("2019-04-24T17:05:00.000")
This output remains same on server and local
But when I am trying to get unix timestamp in milliseconds in the same way
var start_timestamp = moment(start_time, "HH:mm:ss").valueOf();
On server at different timezone
console.log(start_timestamp);//1556125500000
console.log(moment(start_timestamp/1000).format('YYYY-MM-DD HH:mm:ss'); //2019-04-24 17:05:00
On local
console.log(start_timestamp);//1556105700000
console.log(moment(start_timestamp/1000).format('YYYY-MM-DD HH:mm:ss'); //2019-04-24 22:35:00
This start_timestamp value is different on local and server. But timestamp shouldn't change with timezone, it should remains same for all timezones. Please help me with this.
How to get the correct and same value at both places. I got this link some what related to this https://github.com/moment/moment/issues/2035
There is no issue with dates any particular format, issue is only with timestamp.
You need to take the offset into consideration when using moment (using timezones moment.js). Since no offset was passed in the input, the moment will be based on the time zone of the computer the code is running on, hence the different values..
Example:
var a = moment.tz("2013-11-18 11:55", "Asia/Taipei");
var b = moment.tz("2013-11-18 11:55", "America/Toronto");
a.format(); // 2013-11-18T11:55:00+08:00
b.format(); // 2013-11-18T11:55:00-05:00
a.utc().format(); // 2013-11-18T03:55Z
b.utc().format(); // 2013-11-18T16:55Z
If you change the time zone of a moment object using moment-timezone only affects the value of the local time. It does not change the moment in time being represented, and therefore does not change the underlying timestamp.
A Unix Timestamp is always based on UTC - you can see it as the same timestamp at any given location in the world.
Official Moment Docs on timezones
Edit:
If you use utcOffset you should pass an integer:
Example:
moment.utc("2015-10-01 01:24:21").utcOffset("-240").format('YYYYMMDD HHmmss ZZ')
// "20151001 012421 +0000"
moment.utc("2015-10-01 01:24:21").utcOffset(-240).format('YYYYMMDD HHmmss ZZ')
// "20150930 212421 -0400"
MomentJS allows offset-arguments to be passed as string, but it expects the string to be in one of the ISO8601 formats: [+/-]HH:mm or [+/-]HHmm.
To avoid this all together you could, if known, pass the location as an argument like
moment.tz(start_time, "HH:mm:ss", "Asia/Kolkata").valueOf();
as mentioned in the first example above..

Moment.js - formatting an existing time?

I am using moment.js to work with dates and times in node.js. So far I've been able to do everything I need with it, but I am having problems formatting a time.
Here's the scenario:
User enters data (an integer), which is logged in a database, along with date (in the format YYYY-MM-DD) and time (in the format HH:MM:SS).
Next time the user goes to enter data, the previous value is read in and compared (higher, lower or equal to) the new value. However I also want to display a message such as "The last time you submitted your data was at TIME on DATE". In this case, I'd like time to be displayed in a different format (e.g. "h:mm a" i.e. "12:34 pm").
Can I use moment to format an existing date, or can moment only return current date/time? In my code I have the following function:
function userFormattedTime(time)
{
let uTime = moment(time).format('h:mm a');
return uTime
}
However when I call this function and pass it the time (taken from the database), I get "Invalid Time". What am I doing wrong?
You would parse the string from a string back to a moment object, then you can use moment to reformat the date into any other format.
I guess what you are doing wrong is not telling moment what you're sending it back, i.e. it doesn't understand the formatted string you're supplying.
Notice the format values HH:mm:ss which vary in case. The case is important and should be set to match your requirements. https://momentjs.com/docs/#/parsing/
// Original date time string
var rawDateTime = "02-02-2018 10:20:30";
// convert string to a moment object
var originalDate = moment(rawDateTime, "MM-DD-YYYY HH:mm:ss");
// Format a new string from the moment object
var newFormattedString = originalDate.format('h:mm a');
In order to calculate the difference of moment objects you can use the diff function. https://momentjs.com/docs/#/displaying/difference/
// Two different dates
var dateOne = moment("02-02-2018 10:20:30", "MM-DD-YYYY HH:mm:ss");
var dateTwo = moment("04-04-2018 10:20:30", "MM-DD-YYYY HH:mm:ss");
// Get the difference of the two dates
var diff = dateOne.diff(dateTwo);

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