I have this method below that takes in a Javascript date and then has to use Moment js to do its timezone conversion.
QUESTION - Is there a way to accomplish this without moment, just using a JS Date?
// ex. (someDate, 'America/Los_Angeles', 'MMM DD # h:mm A z')
transform(dateUtc: Date, timeZone: string, momentFormat: string): string {
const tempDateUtc = moment(dateUtc).utc(true);
tempDateUtc.tz(timeZone);
return tempDateUtc.format(momentFormat);
}
To apply specific date formatting, rather than a locale based format, then no, not really, but it is possible to get the localized date parts to create a custom format.
As #RobG mentioned, you can use the Intl.DateTimeFormat to create a formatter. You build your options, including your timezone, and then use it's formatToParts(date) method to get the parts you need to construct your output.
Of course, this only works well if your Date is constructed using an epoch or UTC value.
Related
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
Using Luxon JS, I've been trying to format datetime to output in a certain format, using the native toISO function:
This is what I get:
"2018-08-25T09:00:40.000-04:00"
And this is what I want:
"2018-08-25T13:00:40.000Z"
I know that they are both equivalent in terms of unix time and mean the same thing except in a different format, I just want to be able to out the second string rather than the first. I looked through the Luxon docs but was unable to find any arguments/options that would give me what I need.
As other already stated in the comments, you can use 2 approaches:
Convert Luxon DateTime to UTC using toUTC:
"Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.
Use toISOString() method of JS Date.
You can use toJSDate() to get the Date object from a luxon DateTime:
Returns a JavaScript Date equivalent to this DateTime.
Examples:
const DateTime = luxon.DateTime;
const dt = DateTime.now();
console.log(dt.toISO())
console.log(dt.toUTC().toISO())
console.log(dt.toJSDate().toISOString())
console.log(new Date().toISOString())
<script src="https://cdn.jsdelivr.net/npm/luxon#1.26.0/build/global/luxon.js"></script>
From documentation I saw that in the method .fromISO of DateTime you can add an option object after the string of ISO date ("2018-08-25T09:00:40.000-04:00" in your example). In this object specify zone: utc like that:
const DateTime = luxon.DateTime;
const stringDate = "2018-08-25T09:00:40.000-04:00";
const dt = DateTime.fromISO(stringDate, {zone: 'utc'});
console.log('This is your date format', dt.toISO())
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
I have a time in a specific timezone , I want to covert it to UTC . how can I achieve that using moment timezone ?
http://momentjs.com/timezone/
in the documentation this is how to convert :
jun.tz('America/Los_Angeles').format('ha z');
I am just not sure what timezone name to pass to convert it to UTC, or is there another function to use?
You can easily construct a moment in a specific time zone by using the moment.tz(...) syntax. This is slightly different from doing conversions with the .tz(...) function of an existing moment object, which is what you showed in your question.
var m = moment.tz('2016-03-25 08:00:00', 'America/Los_Angeles')
Once you have a moment object, you can convert it to UTC by calling the .utc() function. You can then format it however you like.
moment.tz('2016-03-25 12:34:56', 'America/Los_Angeles').utc().format()
// output: "2016-03-25T19:34:56+00:00"
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
I am calling an API that returns a date/time string such as "2014-04-30 15:32:01". On top of this, I have a known timezone that this date/time exists in. I can see from the javascript Date() class has a .UTC() call for this, but that does not seem to accept a timezone as far as I can tell.
Given the date/time string + timezone, how can I convert those into a UTC timestamp?
I'd recommend using Moment.js and Moment Timezone.
You can create a timestamp using Date.parse from your local time (RFC2822 date format to include the timezone), create a date from that and use Date.toUTCString to get the UTC time. Not sure if it'll work with day light savings though.
Example: http://jsfiddle.net/3vXV6/ (will alert the date)
References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString