there is a situation which I need to convert a date string which consist a timezone abbrevation to a Moment object and parse it. How can I do this? sample date is like below:
var dateString = "2015-01-14 06:57:47 ECT";
be aware that I need the timezone of this date, because Im going to do another conversion to another zone, so if we just consider the date we miss the accuracy.
I don't think moment can handle a timezone name, you should replace ECT with the offset value, like +0200
Related
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' }));
I am trying to convert a datetime string to JS date obj.
My Input is
2021-09-16 19:15:52.930.
The problem is this string doesn't have a timezone, And I need to mark them in cdt/cst(depending on daylight savings) timezone when converting to JS date obj.
The way I am trying to do is:
let dt = new Date('2021-09-16 19:15:52.930 Timezone')
I could pass CDT or CST in placeof timezone, to get it parsed.
But in this case, my logic needs to know which timezone is currently being followed depending on time in year.
I wanted a way so that it can be handled automatically by JS, Like- passing CT? 2021-09-16 19:15:52.930 CT But this doesn't seem to be supported by Date().
If you don't pass anything and use it like this,
new Date('2021-09-16 19:15:52.930')
JS will consider it as your machine's timezone.
If you want it as UTC, add the letter Z like this,
new Date('2021-09-16 19:15:52.930Z')
If you want a specific timezone, add the time difference from UTC. For CDT it is -5:00,
new Date('2021-09-16 19:15:52.930-05:00')
I have a date time string like this 2018-09-10T12:05:00 and I know the timezone associated with the date. Assume timezone available is Asia/Singapore. How can I get the UTC date with this info available?
There's no timezone associated with date string itself. First step would be to associated a timezone with date and then convert it into UTC. Suggested answers are close but do not help my case ?
UPDATE:
Still no luck with this.
Simply just use this method:
var isoDate = new Date('yourdatehere').toISOString();
I am using moment 2.16.0 and want starting days of month. There are different result of toDate() and format() method. Here is jsfiddle.
code:-
var time=moment().subtract(0,'months').startOf("month").format();
console.log(time); //2016-12-01T00:00:00+05:30
var time2=moment().subtract(0, 'months').endOf("month").format();
console.log(time2); //2016-12-31T23:59:59+05:30
var time=moment().subtract(0,'months').startOf("month").toISOString();
console.log(time); //2016-11-30T18:30:00.000Z here i want somethings like 2016-12-01T00:00:00.000Z
var time2=moment().subtract(0, 'months').endOf("month").toISOString();
console.log(time2); // 2016-12-31T18:29:59.999Z here i want somethings like 2016-12-31T59:59:59.000Z
All of your operations are using moment with local time except the toISOString, which will give you the string in UTC. Since your timezone is offset from UTC, naturally the local time string (from format) and the UTC time string (from toISOString) are very different.
here i want somethings like 2016-12-01T00:00:00.000Z
That would be a different time from what that Moment instance represents.
If you want something in an ISO-8601 format but in local time, you can use format with the appropriate set of formatting tokens, but you don't wan the Z at the end because, again, you're not dealing with UTC ("Zulu") time, you're dealing with local time.
moment().format("YYYY-MM-DDThh:mm:ss.SSS")
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