Is there any way to generate Date object in UTC timezone - javascript

I have a typescript method that expects a Date object and it converts into a local timezone using moment. Now I want to write a unit test for that and to accomplish that I need a Date object in UTC format.
The problem is when I do new Date() to get date object it returns date with local timezone
Wed Apr 15 2020 11:38:10 GMT+0530 (India Standard Time)
and since the zone already local my method won't convert it into local again.
convertUTCtoLocal(utcDate: Date): Date {
return new Date(moment.utc(utcDate).local().format());
}
I know there are methods like .toISOString(), .toGMTString() but these return a string I want a Date object
Any idea, what can be done.

Related

How to convert JSON time stamp in javascript to date and time?

I am getting Time stamp as "2020-03-02T05:50:31.000Z"
How to convert this to Normal Readable Format with Date and Time Zone
const parsedDate = new Date("2020-03-02T05:50:31.000Z");
console.log(parsedDate.toGMTString())
//"Mon, 02 Mar 2020 05:50:31 GMT"
console.log(parsedDate.toLocaleString())
//"3/2/2020, 11:20:31 AM"
console.log(parsedDate.toDateString(), parsedDate.toTimeString())
//Mon Mar 02 2020 11:20:31 GMT+0530 (India Standard Time)
In java-script above date format can be parsed by using Date.
Eg:
var date = new Date('2020-03-02T05:50:31.000Z');
I don't know what you mean readable format but you can use following methods:
new Date('2020-03-02T05:50:31.000Z').toLocaleString();
// outputs date according to user locale settings
Or you can use getYear, getMonth, getDay methods to get them and format date as you want
You can use moment.js for date time format and conversion.
Pass your date timestamp as below:
moment.parseZone("2020-03-02T05:50:31.000Z").format("DD-MM-YYYY hh:mm:ss z Z")
Modify format as you need ref mentioned here
Use in your code as mentioned here

Get time in London no matter of local time [duplicate]

I want to convert a local date object to a date object in another timezone and this is what I have:
moment("2016-08-04T23:30:37Z").tz("Asia/Hong_Kong").format("M/DD/YYYY h:mm a")
>>"8/05/2016 7:30 am"
but if I do
moment("2016-08-04T23:30:37Z").tz("Asia/Hong_Kong").toDate()
>>Thu Aug 04 2016 16:30:37 GMT-0700 (PDT)
As you can see, I can format the moment object to however I like, but how do I return it to a date object again?
... to a date object in another timezone
The JavaScript Date object cannot represent another time zone. It is simply a timestamp, measured in milliseconds since 1970-01-01 midnight UTC, which you can see with .valueOf() or .getTime().
When you call .toString() on a Date object, or otherwise coerce it into a string (such as when displaying it in the debug console), it converts the timestamp to the local time zone where the environment is running.
Therefore, despite any conversions you do with moment-timezone, you are still talking about the same moment in time, and thus will have the same timestamp in the resulting Date object.
In other words, these are all equivalent:
moment("2016-08-04T23:30:37Z").toDate()
moment.utc("2016-08-04T23:30:37Z").toDate()
moment("2016-08-04T23:30:37Z").tz("Asia/Hong_Kong").toDate()
new Date("2016-08-04T23:30:37Z")
... because they all have the same internal timestamp of 1470353437000
moment("2016-08-04T23:30:37Z").valueOf() // 1470353437000
moment.utc("2016-08-04T23:30:37Z").valueOf() // 1470353437000
moment("2016-08-04T23:30:37Z").tz("Asia/Hong_Kong").valueOf() // 1470353437000
new Date("2016-08-04T23:30:37Z").valueOf() // 1470353437000

Issue with jQuery plugin datetimepicker not being recognized as date by Date() object wrapper

I have an element where the user is able to pick a date and time using this plugin:
https://github.com/xdan/datetimepicker/
I am using the following configuration options:
$('#startDate').datetimepicker({
format: 'Y-m-d\\TH:i:s',
});
The output:
$("#startDate").val()
returns:
"2016-02-18T23:59:00"
When I attempt to cast this string to the Date() object type like so:
Date("2016-02-18T23:59:00")
Date("2016-02-18T23:59:00Z")
Date("2016-02-18T23:59:00+00:00")
Date returns this (The current date/time):
"Wed Feb 17 2016 14:02:43 GMT-0600 (Central Standard Time)"
How do I get the datetimepicker to return a value that is recognized by the javascript Date() method, or how do I manually convert the returned date to a format recognized by the javascript Date() method?
try
var d = new Date("2016-02-18T23:59:00");

Javascript new Date decreases date by a day

I have a scenario where I am using AngularJS to read date. Interestingly it decreases my date value by one.
Why is this happening?
new Date("2016-01-06T00:00:00")
give me result as
Tue Jan 05 2016 16:00:00 GMT-0800 (Pacific Standard Time)
This is because when you use the new Date() in the JavaScript, it converts and prints the date in browsers timezone.
So if you print:
new Date("2016-01-06T00:00:00-0800")
You will get the actual output you want, because of the -0800 difference between your time zone (determined by the browser) and the UTC time.
The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information (note that ECMAScript 2015 specifies that date time strings without a time zone are to be treated as local, not UTC).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Since your date string appears to lack one, JS assumed it's UTC time. The result you see is the same moment in time, offset to your timezone. All you need to do is provide timezone data to the string you're parsing.
It is because the date is taking your browser's timezone in to account, which in your case is in PST. It does the same to me, in EST:
test = new Date("2016-01-06T00:00:00")
Tue Jan 05 2016 19:00:00 GMT-0500 (EST)
You can still obtain the time in UTC by using any of the .getUTC* functions, like so:
test.getUTCDate();
6

new Date() converts timezone

Why is new Date() converting the timezone? I'd like my date to be the same as the string I provide, so 00:30 and not 10:30.
>>> new Date("2015-04-11T00:30:00");
Sat Apr 11 2015 10:30:00 GMT+1000 (AEST)
You passed the date in ISO form into the constructor "2015-04-11T00:30:00".
That means your browser interprets that not as local time but as UTC. Date.toString however uses your local time. If you want to use UTC time call .toUTCString or better yet .toISOString.

Categories