I'm writing javascript multiplayer game, my server code is in nodejs.
I need to synchronize time between server and client.
At server side i call Date() and send the date string to client.
Client recieves:
Tue Apr 28 2015 15:37:01 GMT+0000 (UTC)
After initialization with:
var time = Date("Tue Apr 28 2015 15:37:01 GMT+0000 (UTC)");
the time variable set to local time is:
Tue Apr 28 2015 17:37:03 GMT+0200 (CEST).
If i run my server localy then time is importing correctly.
Is problem in different time format, CEST vs UTC ?
Thank you for answers
There are various ways to handle the timezones in JS.
Set UTC time across system
use .setUTCHours()
Here you can see how to use it.
Create a new UTC Date
new Date(Date.UTC(year, month, day, hour, minute, second))
Date.UTC is explained here
Correct the difference in timezones manually
var d = new Date(xiYear, xiMonth, xiDate);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
You can send from server date in milliseconds format and then converting milliseconds to the date in required format.
Server:
var time = new Date().getTime();
Cleint:
var date = new Date(time);
Related
my js file looks like below
var today = Date();
console.log(today); // Thu Aug 04 2022 15:28:52 GMT+0530 (India Standard Time)
var today = new Date();
console.log(today); // 2022-08-04T09:58:52.640Z
I have set the local timezone in my centos to Asia/Calcutta and its Date() function is displaying correctly as Aug 04 2022 15:28:52 , how do I make even new Date() to output the same data in my nodejs script.
I think what you're looking for is pretty well described on this topic : How to initialize a JavaScript Date to a particular time zone
To summarize, you only have to do today.toLocaleString('IN', { timeZone: 'Asia/Kolkata' }) to get the time in India.
Please note that I'm not sure I picked the good timezone, so you might need to adjust that.
app.get("/test",function(req,res){
var d = new Date();
res.send(d);
});
This give output "2019-03-19T04:50:47.710Z" which is UTC when i visit through mydomain/test
app.get("/testejs",function(req,res){
res.render("testejs");});
My testejs below-
<%= new Date() %>
This ejs when visited through mydomain/testejs gives output -
Tue Mar 19 2019 12:55:52 GMT+0800 (Singapore Standard Time)
How is this possible that new Date() give me two different output from( app.js and testejs.ejs ) but it is executed on same server?
new Date().toLocaleString()
Click here Out put is there
OR
new Date(Date.now()).toLocaleString()
Sample Out Put nodejs backend and frontend ejs using date function 3/19/2019, 10:58:31 AM
Good day,
Remember that app.js is running on the server side and ejs on the browser, the browser date format is different from the server.
new Date() generates a date value, which is a number of milliseconds since a base time, usually Jan 1, 1970. The exact value depends on the setting of the clock on the server where the value is generated.
That value can be displayed in various ways.
Depending on the default settings of the client environment,
that my be in UTC, or the local [or some other] time zone,
and in various formats.
Note that 04:50 UTC and 12:55 GMT+0800 are about 5 minutes apart,
but in different time zones. If this was two tests run 5 minutes
apart, that would explain the difference.
Instead of using the default toString() method to display the times,
use getTime() to see what the raw number is.
They both are not two different dates but the same date in different timezones.
Reason for the different format is how the toString() method is written for the Date object,
the toString() method is written in a way to return the UTC formatted date so when you send it on the browser it calls the toString for the date as everything will be converted into a string before mounting.
new Date() invokes the constructor and return the date instance which also will have all its methods, however, Date() is a function which returns the stringified date.
See the examples below
date = new Date('Tue Mar 19 2019 12:55:52 GMT+0800')
console.log(date)
console.log('toString-%s',date.toString())
console.log('toString-%s',''+date)
console.log('Tue Mar 19 2019 12:55:52 GMT+0800')
//Managing offsets
console.log(Date('Tue Mar 19 2019 12:55:52 GMT+0800'))
date.setTime(date.getTime() + (8-5.5)*60*1000 );
console.log(date)
I've seen various versions of this question, but none of them answer my needs.
I want to create an ISODate for MongoDB and I'm using Node.js.
In Node, when I do:
console.log(Date());
I get:
Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) <-- This is correct.
When I do:
console.log(new Date());
I get:
2016-09-26T19:17:04.731Z <- This is 4 hours ahead
My understanding of the way to do ISODATE is:
var isodate = new Date().toISOString()
console.log(isodate);
Which yields a time 4 hours ahead of "now".
My system date is correct.
I run this one different machines, and I get the same results.
Can someone please explain why I'm getting a discrepancy in time?
The difference is that 2016-09-26T19:17:04.731Z related to GMT0 timezone and Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) to your local timezone. Both are point to the same time :)
You can read more about data formats and timezones in Wiki
With a basic definition to the difference between Date() and new Date()
is :
Date() ignores any argument(s) passed to it and is equivalent of new Date().toISOstring()
new Date(Optional_arguments) creates an time type object in JS on which you can perform :
getTime()
other Date.prototype functions listed on MDN Website
Date() is just a string representation of local time.
new Date() gives you a manipulatable object to fiddle around.
Notice the Z at the end of 2016-09-26T19:17:04.731Z?
It stands for Zulu, meaning UTC timezone (which is GMT+000).
As you can see in your original date string, Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) has a GMT-0400 timezone, which I guess is the local time where you live.
So, in fact there is no problem, just different representations of the same time:
Date() creates a Local date
new Date() creates a UTC date
Given I have the number 1446309338000, how do I create a JavaScript UTC date?
new Date(1446309338000) will equal a CST time (central standard) or local time.
new Date(Date.UTC(year, month, day, hour, minute, second)) haven't got this info yet.
Does JavaScript change the time if I do this?
new Date(1446309338000).ISOString();
Is it creating a new CST date and then converting it to UTC? I really just need the string. I am taking it from a database (RowKey from a Azure Table storage database).
If you have the milliseconds that's already the UTC date. Which basically means the universal time. Now based on those millis you can convert the Date object into a String of your like:
new Date(1446309338000).toUTCString() // timezone free universal format
> "Sat, 31 Oct 2015 16:35:38 GMT"
new Date(1446309338000).toString() // browser local timezon string
> "Sat Oct 31 2015 09:35:38 GMT-0700 (PDT)"
new Date(1446309338000).toISOString() // ISO format of the UTC time
> "2015-10-31T16:35:38.000Z"
Now, if for some reason (I can't see a valid reason, but just for the heck of it) you're looking for having a different amount of milliseconds that represent a different date but that would print the same in the local browser timezone, you can do this calculation:
new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000))
Now toString from original Date and toUTCString of this new Date would read the same up to the Timezone information, because of course they're not the same date!
new Date(1446309338000).toString()
> "Sat Oct 31 2015 09:35:38 GMT-0700 (PDT)"
new Date(1446309338000 - new Date(1446309338000).getTimezoneOffset() * 60 * 1000).toUTCString()
> "Sat, 31 Oct 2015 09:35:38 GMT"
It's actually as simple as homemade biscuits, If you have your date, say:
var date_in_milliseconds = 1504640419000;
You can then initialize a new date like this:
var human_readable_date = new Date(0); //Date(0) creates a date at the Epoch, so Wed Dec 31 1969
now, just add the milliseconds to the Epoch, and this will give us the desired date:
human_readable_date.setUTCMilliseconds(date_in_milliseconds);
Well, if the date string is what you require, hope this helps:
new Date(1446309338000).toLocaleString('en-US', {timeZone: 'UTC'})
As far as toISOString() is concerned, it returns string representation using ISO-8601 standard (the format is: YYYY-MM-DDTHH:mm:ss.sssZ).
toLocaleString() is human readable format with same result.
I'm trying to get the current midnight date with Javascript and I use this:
var mydate = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 0, 0, 0, 0);
I receive this:
Sun Oct 26 2014 00:00:00 GMT+0100 (BST)
The problem is, that it's not British Summer Time anymore, but for some reason javascript still thinks it is. This is messing up my entire application, as I convert this into a timestamp like this:
mydate = Math.round(to_day.getTime() / 1000);
How can I change this?
JavaScript will always use your computer's locale to extract the date. It's using BST because this is the timezone that you have set (automatically perhaps?).
You can extract a UTC time string by calling the following function:
var d = new Date();
d.toUTCString(); // "Sun, 26 Oct 2014 11:46:11 GMT"
There are libraries that deal specifically with manipulation of date objects. One such tool I recommend you checkout is moment.js. It gives you the option to convert between timezones as well as a whole lot of other useful time/date related features.