how to set time zone in javascript new Date() [duplicate] - javascript

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm using javascript new Date format. It is working well in android and windows but when I use it on iphone it outputs different result. How can I be able to solve the problem?
var rawGetDate = "2019-02-01 00:00:00";
var dateF = new Date(rawGetDate.replace(' ', 'T'));
console.log(dateF);
OutPut in Androaid: Tue Jan 01 2019 00:00:00 GMT+0600
OutPut in Iphone: Mon Dec 01 2018 00:00:00 GMT+0500

Related

JavaScript .toISOString() function returning incorrect date [duplicate]

This question already has answers here:
toISOString() return wrong date
(3 answers)
Format JavaScript date as yyyy-mm-dd
(50 answers)
Closed 4 months ago.
Here is my Cypress JS code:
cy.get('creationDateElement').then(date => {
const reformattedDate = new Date(date.text())
cy.log('Reformatted Date: ' + reformattedDate)
cy.log('ISO string: ' + reformattedDate.toISOString().split('T')[0])
})
I am trying to transform the date so that it returns 2015-09-11
Here is what is logged with the above code:
Reformatted Date: Fri Sep 11 2015 00:00:00 GMT+0100 (British Summer Time)
ISO string: 2015-09-10
As you can see, the day being returned is 10 rather than 11, but I don't know why this is happening.
Can someone please tell me why this is happening, & how I can return 2015-09-11 instead?
The code is working with the below dates though, so I don't know why the one above is causing an error:
Reformatted Date: Wed Feb 28 2007 00:00:00 GMT+0000 (Greenwich Mean Time)
ISO string: 2007-02-28
This is part of a Cypress UI test. So the actual date comes from an element on the UI that contains a date:

Showing correct date from API [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 5 years ago.
I'm getting crazy with showing a date correctly to the client:
From the API I receive data.expiry_date and its value is: 2017-09-06T23:59:59Z
The client to show that data looks like this:
var date = new Date(data.expiry_date);
$('#expiry_date').val(`${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`);
The result is a day before than expected: it should be 06/09/2017 but it show 07/09/2017.
Basically the date value from the action var date = new Date(data.expiry_date); is: Thu Sep 07 2017 01:59:59 GMT+0200 (CEST).
How can I get rid of it?
You're not in the same time zone.
2017-09-06T23:59:59Z is UTC
which is same as
Thu Sep 07 2017 01:59:59 GMT+0200 (CEST)

How to simplify javascript date object? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 6 years ago.
I create this date object:
var date = new Date(06, 2016);
When I open browser watch to check the created object I see this:
Mon Jun 06 2016 12:51:11 GMT+0300 (Jerusalem Daylight Time)
How can I simplify the above date object to this:
2016-06-06
You could do this...
date.toISOString().split("T")[0] // "2016-06-06"

Why is new Date() removing a day? - Javascript [duplicate]

This question already has answers here:
Why isn't "2016-02-16" equal to "2016-02-16 00:00"?
(5 answers)
Closed 6 years ago.
I am creating a date with new Date(). When I do this, it is subtracting a day. Here is the code:
var dateString = "2016-04-10";
var date = new Date(dateString);
// date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT)
What do I misunderstand? Why is the date not Apr 10 2016? How do I make this work properly?
Your timezone is GMT-6 (as revealed by the GMT-0600 (MDT) in the output you've provided). Therefore the date which gets generated is offset by -6 hours. In this case, midnight minus 6 hours is 6PM on the previous day.
If you call date.toISOString(), you'll see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.

javascript new Date() issue [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 8 years ago.
I'm facing a date issue with new Date() function. May i know why is this weired output is coming.
new Date(1984, 12, 13)
Sun Jan 13 1985 00:00:00 GMT-0500 (EST)
Here the month should be December but it results me with Jan.
January is 0 in javascript , so javascript is counting up and is adding the date. That's why you get this result.

Categories