This question already has answers here:
How to convert Moment.js date to users local timezone?
(5 answers)
Closed 6 years ago.
I am building an app using node.js + postgresql. My postgresql stores dates in the format:
2016-04-01T04:00:00.000Z
When the date is returned to the browser, I want the date to be returned in the timezone of the user. Is there a way I can do this? I found moment.js but I'm not sure if it actually detects a user/browser's timezone for the conversion or not...
Can someone help? Thanks in advance!
If you want suport multitimezone, you should have in db unix timestamps.
Moment js Is really good lib for an time calculation.
Moment js can detect browser timezone.
Of course you always use vanilia like
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
Related
This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 3 years ago.
I need to convert from one timezone to another timezone in my project.
I am able to convert from my current timezone to another but not from a different timezone to another.
For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.
However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.
The easy and fast way is to use an external library. I recommend moment.js. once installed you can convert pretty easily and reliably through their ready-made functions
This question already has answers here:
What is equivalent of DateTime.ToOADate() in javascript?
(7 answers)
Closed 4 years ago.
I need to pass a Date value to a .Net server which expect to receive OLE Automation Date value (Microsoft Timestamp - a decimal value).
Any idea how to convert a Javascript Date to this format in JS?
Thanks in advance.
You can use momentjs but I don't think that's a good practice. I think it is better to pass an standard date format (like ISO) and then in your .NET application convert it to OLE Automation Date format, this way the front part is not coupled to your database.
This question already has answers here:
Are Javascript date/time functions dependent on the client machine?
(5 answers)
Closed 8 years ago.
Consider this line of code:
var currentTime = new Date();
Did it look at my IP address? Did it talk to my local machine?
How javascript determines what current time in my time zone is?
Since javascript is a client-side language, the Date() method implemented in the browser is querying the client machine for the current time. If you want a date you can trust, query a server for a timestamp and use that in your calculations.
Relevant answer
The JavaScript parser asks the browser, which asks the computer, which looks it up internally. Most languages have a feature for date/time.
It uses the timezone that's configured in the OS the browser is running on.
The JavaScript Date() function returns number of milliseconds from 1 January, 1970 UTC.
If no arguments are provided to the constructor (new Date()), the constructor creates a Date object for the current time according to your system settings
This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 9 years ago.
I have seen a lot, really a lot of post to find out a solution for my problem, but i couldn't get it, so decided to create a question.
My Question is how do we actually create a new javascript date object in different timezone, not in local timezone
i know we could create local date object and convert it to different timezone in number of ways, but i dont want to convert instead i need to create in specific timezone.
here is simple example for my problem,
Say, user has choosen a timezone "America/New_york", so all the dates in a calendar page will be shown in that timezone.
Now, if we create a event at "05:00 pm" , how do we actually create date with time 5 pm in "America/New_york" timezone,
if we use new Date() (assume browser is in different timezone say "Asia/Kolkata"), then converting it to "America/New_york" will not get "5:00pm" in that timezone , instead it will get corresponding time of "05:00 pm IST" in that timezone which will have different hour & minute value.
Any suggestion would be helpful!
Thanks
The short answer is you can't.
A Date object is just an accessor to the system time settings (so it will use the local computer timezone anyway). You can then manipulate your dates by substracting the local timezone using getTimezoneOffset(), or forcing a time with setUTCHours().
Note that moment.js is a good alternative to handle dates and timezones: http://momentjs.com/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript/PHP and timezones
It is possible to get the Olson timezone id from javascript on the client's machine e.g. (America/New York). I know that PHP can do this via the timezone object, something like this. $timezone->getLocation(); Does similar functionality exist for JS?
I know that you can grab the timezone offset of the client as followed:
var curdate = new Date();
var offset = curdate.getTimeZoneOffset();
But I need more granular information provided by the Olson Id. Thank you for your help.
You can't directly access timezone in JavaScript. You can, however, measure offsets reported at several different specific dates to deduce what exactly time zone is in use by comparing regular and daylight savings times to database of zones. There's a jsTimezoneDetect library that can do most of this work for you.