How javascript determines what current date is? [duplicate] - javascript

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

Related

React : new date() shows date of client machine rather than server [duplicate]

This question already has answers here:
Which time zone does JavaScript `new Date()` use?
(4 answers)
Closed 4 years ago.
I have a react app running in a server with different locale where I am using
new Date()
When opening app from my client machine it returns the date of client locale instead of server locale ,
Reason ???
let currDate = new Date();
That is how javascript date works. Please refer to MDN docs for Date for more details. Also checkout the the parameters you can send to customize it.
If no arguments are provided, the constructor creates a JavaScript Date object for the current date and time according to system settings for timezone offset
Where Date is called as a constructor with more than one argument, the specified arguments represent local time
The JavaScript Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.

Convert to datetime to local timezone javascript [duplicate]

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

Best way to deal with dates based on location. SQL/JS [duplicate]

This question already has answers here:
How can I handle time zones in my webapp?
(7 answers)
Closed 9 years ago.
Simple scenario:
John Doe lives in California and posts a comment.
Jane Doe lives in Maryland and views the comment.
I'm used to dealing with local based websites and usually use GETDATE(). What's the best way to deal with this so that John and Jane see the date based on their time location? Do I send a parameter to the SQL query based on a javascript function to grab their timezone? Is there a better way?
A good example is Facebook. How do they deal with all the different time zones? Is it client side? server side? etc.
Thanks!
The handling is usually two fold. To handle it on server side, you'll need reliable time zone information. This is usually obtained from the user's preferences for time zone in their profile page.
Having said that we need to make sure that all date data captured on client side and when stored in DB is in UTC format. JS functions have a ready support for the UTC datetime.
When you store you use UTC() method in JS to get date.
When displaying back either you supply the time-zone equivalent from DB(based on stored user time zone data) or use JS to get it from the browser locale like this
<script type="text/javascript">
function showDateInClientSideFormat(dValue)
{
var d = new Date()
var n = d.getTimezoneOffset();
var dateClientSide = new Date(dValue +n);
return dateClientSide;
}
</script>
Also see my answer here https://stackoverflow.com/a/21573366/1123226

Create New JavaScript Date Object With Different Timezone During Initialization [duplicate]

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/

Getting Olson timezone ID information from browser via javascript [duplicate]

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.

Categories

Resources