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
Related
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 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.
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")
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
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