How to get client's timezone [duplicate] - javascript

This question already exists:
JavaScript timezone information how to get America/Los_Angeles (or equivalent) [duplicate]
Closed 6 years ago.
I looked into the internet but all I could find was getTimezoneOffset() javascript function. But it gives me offset not the timezone, the client is in. Please guide me.

Did you try the getTimezoneOffset() this way?
var offset = new Date().getTimezoneOffset();
console.log(offset);
Because it works for me.

If you are looking for timezone specifically.
var date = new Date();
console.log(date.toString());
//Wed Nov 09 2016 22:21:53 GMT+0530 (IST)
console.log((/\((.+)\)/g).exec(date.toString())[1]);
//IST

Related

How to get day relative to current timezone using Date in Javascript? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
new Date('2019-01-01')
Mon Dec 31 2018 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2019-01-01').getDate()
31
I would be expecting 1 to be the result. How can I get day relative to current timezone using Date in Javascript?
The constructor appears to set the Date object's value using the UTC time that corresponds to the string argument (midnight on 2019-01-01) -- for which the local equivalent is Mon Dec 31 2018 19:00:00 GMT-0500 (Eastern Standard Time).
Storing local midnight would mean actually storing 5AM UTC, like:
new Date('2019-01-01T05:00:00');
Since we don't necessarily know the difference between local and UTC times in advance, we can find and use it dynamically like this:
let date = new Date("2019-01-01");
let offset = date.getTimezoneOffset(); // Returns the offset in minutes
date = new Date(date.getTime() + (offset * 60 * 1000)); // Adds the offset in milliseconds
console.log(date.toLocaleString());
For further reference,
- Here's a somewhat-related question (where the top answer actually recommends importing a library to handle these issues): How to add 30 minutes to a JavaScript Date object?,
- And here's good-ol' MDN's page on JS dates:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If you populate the Date constructor with the timezone offset, you can instead use getUTCDate.
var date1 = new Date('August 19, 1975 23:15:30 GMT+11:00');
var date2 = new Date('August 19, 1975 23:15:30 GMT-11:00');
console.log(date1.getUTCDate());
// expected output: 19
console.log(date2.getUTCDate());
// expected output: 20

Constructing a date in a specific timezone [duplicate]

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
How do I specify the time zone when creating a JavaScript Date?
(1 answer)
Closed 5 years ago.
Without using moment.js, is there a way to construct a date in a specific timezone.
For example, I have two strings:
var date = '2018-01-23';
var time = '12:00';
I can construct the date like so:
var constructedDate = new Date(date.substring(0,4), date.substring(5,7)-1, date.substring(8,10), time.substring(0,2), time.substring(3,5));
which provides output of:
Tue Jan 23 12:00:00 GMT+00:00 2018
However, I am looking for output in a particular timezone (e.g +11:00)
Tue Jan 23 12:00:00 GMT+11:00 2018
Alternatively, is there a way to offset the date -11 hours so when I use the GMT date in my application it will be correct.

Date in javascript show a day before of what it's really [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 6 years ago.
Anyone knows why this happens? I read (from a database) a date (2016-10-05), but Javascript create the object like 2016-10-04.
Can be anything related to the timezone? Can someone point me to some source that let me help to figure it this? (I have search, but to be honest, not knowing what the issue is, it's difficult to look for more info)
The time you've specified is at midnight GMT or 0 hours. The EDT timezone is 4 hours behind GMT, so when you convert the time to the EDT timezone (your local timezone), it's -4 hours into the previous day.
I find some info in MDN Date page, use the d.toUTCString() method can turn to GMT.
var d = new Date('2016-10-05T00:00:00.000+0000');
d.toUTCString();
return "Wed, 05 Oct 2016 00:00:00 GMT"
Hope this can help you

Why is date being set one day earlier than expected? [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 6 years ago.
var dateTest = new Date('2015-03-31');
console.log(dateTest);
Result:
Mon Mar 30 2015 20:00:00 GMT-0400 (Eastern Daylight Time)
However I expected this to be March 31. How might I do this?
Because it's setting the date according to UTC, and returning it with your local timezone offset. I'd recommend explicitly specifying the timezone offset, if that's what you need, or adding your timezone offset after the fact.
var d = new Date('2015-03-31T00:00:00-0400');
// or
var d = new Date('2015-03-31');
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
If you want it to be in UTC, you can call the toUTCString() method on it instead, which will give you the date you expect, albeit not in your timezone.

change this date format to work with new date() [duplicate]

This question already has answers here:
javascript: how to parse a date string
(4 answers)
Closed 8 years ago.
I get a date returned from an API in the format:
var date = result.posted //gives 2014-03-29 02:07:26
when I run
new date(date)
on desktop it works fine
but on mobile, I get the error 'invalid date'
how can I make this work across the board - I want to compare it to the current time ( var cur_time = new date() )
I suggest you to use moment.js, it is very easy to compare date. In your case:
moment("2014-03-29 02:07:26", "YYYY-MM-DD HH:mm:ss").fromNow();
Try this..
var date = new Date(Date.parse(parseInt(result.posted)))
it will give date object like below
Date {Wed Jan 01 2014 05:30:00 GMT+0530 (India Standard Time)}

Categories

Resources