Set timezone in javascript without toLocaleString - javascript

I want to set a timezone to a date. So basically a date should be display everytime taking into account that timezone. Also i want to use a native solution.
I know that exists this solution:
const t = new Date(1641991591447).toLocaleString('en-GB', { timeZone: 'Europe/London' })
console.log(t)
But this returns 12/01/2022, 12:46:31 instead of 2022-01-12T12:46:31.447Z this format. So how to get the last format taking into account the timezone?

In order to convert your date to the specified format (ISO date string), you can do this by simply creating a Date constructor and using your date value as the input and chaining the toISOString method to it.
const t = new Date(1641991591447).toISOString(); // 2022-12-01T12:46:31.000Z

Related

How to display times in different time zones using the offset value

I am trying to display the current moment in different time zones. I have tried to use native javascript and the moment-js package but it seems you require the time zone name (ex. "America/Toronto") to display the information I want. The problem is that the information I currently have is the timestamp in string format (see below for string format) from the desired timezone, and the city the timestamp belongs to. The problem with using the city to create my tz value is that one of the cities I want to display isn't in the IANA tz database (Calgary).
String timestamp:
2022-04-26T14:19:42.8430964-04:00
As can be seen I do have the time offset and I was hoping there was a way to convert this string in js to a Date object and then display the time using the offset in the correct time zone.
Note what I want to display is the following format:
12:19 pm MT
I know I could just parse out the string but I feel like this isn't the best practice, however I may be wrong.
Also note that I will have to get the time zone (ex. MT, ET it can also be MST, EST) using the offset.
you don't need moment here. JS can do this natively. Also moment is now deprecated.
Your IANA timezone is America/Edmonton. So it's simple to do. That date format is an ISO 8601 date format, so you can just pass it into the new Date constructor and it'll parse it correctly:
const iso8601 = '2022-04-26T14:19:42.8430964-04:00';
const date = new Date(iso8601);
console.log(date.toLocaleString('en-US', { timeZone: 'America/Edmonton' }));
It doesn't matter what timezone your input date is set, so long as it has the correct offset in the ISO date. Once it's a Date, the offset is irrelevant. E.g.:
const iso8601 = '2022-04-26T14:19:42.8430964Z';
const date = new Date(iso8601);
//time will now be 4 hours off above as the input date is UTC
console.log(date.toLocaleString('en-US', { timeZone: 'America/Edmonton' }));

JS How to change timezone without changing locale (time format)

I get time from backend as number.
I set the date by:
const time = 1571307720000;
const date = new Date();
date.setTime(time);
date.toLocaleString();
I will get timezone from backend and I want to set it in toLocaleString() but I don't want to change locale(time format) but in every page I see:
date.toLocaleString('es-US', { timeZone: 'Asia/Seoul' })
and I can't use date.toLocaleString function without first string.
Is it possible to set timezone but use locale time format?
According to the MDN entry for Date.prototype.toLocaleString(),
See the Intl.DateTimeFormat()constructor for details on these parameters and how to use them.
And the entry for Intl.DateTimeFormat() says,
To use the browser's default locale, pass an empty array.
So:
date.toLocaleString([], { timeZone: 'Asia/Seoul' })

Compare date string coming from one timezone with Date object in another timezone

I do have an array of objects coming from API, each of the object containing a property called jpExpiryTime.
[{ ...
offer: '',
jpExpiryTime: 2019-09-26 15:00:00
},
{ ...
offer: '',
jpExpiryTime: 2019-09-26 15:00:00
}]
The above date and time value are assigned from the Japan region. I do have a portal which is accessed from India which shows me the list of the above offer data. I want to know if the above offer has expired or not by comparing with the current date and time. I tried the below:
new Date('2019-09-26 15:00:00') > new Date ()
But I find that new Date('2019-09-26 15:00:00') converts the data to IST rather than Japan Standard Time (JST). Could someone shed some light on how to compare a date-time string from one timezone with another date object in another timezone.
You are using normal JS date comparison and conversion. JS is executed client side browser and if its opened in INDIA then it will convert in IST
use Moment js which is very popular.
Convert your date string using timezone and then compare. Everything is available.
isSame, isBefore, isAfter etc.
You can do something like below: First convert the time to "Asia/Tokyo" timezone and then compare.
var japanTime = new Date("2019-09-26 15:00:00").toLocaleString("en-US", {timeZone: "Asia/Tokyo"});
convertedJapanTime = new Date(japanTime);
var currentTime=new Date();
var isexpired=currentTime>convertedJapanTime ;
Timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

constructing a date object along with time zone

I want to construct a Date object along with dynamically selected timezone. I am currently in IST time zone. I want to eliminate the usage of Date.parse() as it does not behave as expected at times.
let's assume tzOffset to be +05:30 for now. It could be any other timezone based on what users want. new Date(epochDate).toISOString(); converts the date to UTC timezone. How do I get the date in toISOString() format but also get it in the desired time zone
const tsConstruct = `${year}-${month}-${date}T${hour}:${min}:00${tzOffset}`;
const epochDate = Date.parse(tsConstruct);
scheduledTs = new Date(epochDate).toISOString();
JavaScript's Date does not store timezone info. It just stores the number of milliseconds from UNIX EPOCH. Then, depending if you use the UTC methods or not, it returns the date and time in UTC or the local time.
You should have to change the date and time, based on the timezone indicated, to UTC or local time, and then store it in a Date object. But, of course, to show the stored time in another timezone different to the local one or UTC, you must do the conversions yourself, so, as #RuChengChong suggested, use a helper library like momentjs.

Convert string to new Date object in UTC timeZONE

Can anyone let me know how to convert a string to a date Object with UTC time zone in ExtJs?
String is "2015-10-07T23:59:00". I would like to get the same in Date Object without changing the timezone.
First of all, your date string does not have a timezone.
When you make a JavaScript date object from a string, there are two possible outcomes you could expect:
You may want the date to be 23:59 Local (23:59 CEST in my case).
In this case, you want to use new Date("2015-10-07 23:59:00") with plain javascript (note the missing T), or Ext.Date.parse("2015-10-07T23:59:00","c");.
You may want the date to be 23:59 UTC (e.g. 01:59 CEST).
In this case, you want to use new Date("2015-10-07T23:59:00").
Of course, whenever you output the date, you have to get the date in the correct time zone as well. The console/toString will usually show it in local time. JavaScript does provide getUTC... methods if you require other time zones.
You see, using Time Zones with JavaScript is a painful experience. I would recommend to try moment.js if you need full time zone support.
You can use Ext.Date.parse.It gives Date Object as output.It syntax is:
Ext.Date.parse( String input, String format, [Boolean strict] )
For Example:
Ext.Date.parse("2015-10-07T23:59:00", "Y-m-dTH:i:s");
try
var millisFromEpoch = Date.parse('2015-10-07T23:59:00');
it will parse date in GMT timezone, Ext.date.parse use the current timezone instead

Categories