I want to find time difference between user's time and server side time. Like what time is user having on his system and what's the value coming from database. I have got it approximately.
But now my problem is what if user is from difference time zone rather then the value is coming from database? So now I want to find the exact difference by managing their time zones. Here I am putting my sample code and the values I am getting with their formats.
var studentSessionStartTimeDate = that._sessionData.studentSession.dtStart; //date with time from db
var splitTimeDate = studentSessionStartTimeDate.split(' ');
var oldDate = splitTimeDate[0]; // split date different
var dateold = new Date(oldDate).getTime(); //convert date into milliseconds
var time=that._sessionData.studentSession.start; // User's exact start time from db
var convertedTime=(Number(time.split(':')[0])*60+Number(time.split(':')[1]))*1000; // converted time in milliseconds
var sessionStartTime = dateold+convertedTime; // db time and date value in milliseconds
var systemTime = new Date().getTime(); // user's system time value in milliseconds
var timeDiff = sessionStartTime - systemTime; // now i want this time diff to be exact but it is not calculating the zone difference
Now if someone want I can get utc Offset from db also, but how can I get difference?
you could get browser time zone with following code:
new Date().getTimezoneOffset();
Related
i am trying to get current date to compare and setting hours to zero but still getting time.
var today = new Date(new Date().setHours(0,0,0,0));
var todaynew = today.toISOString();
console.log(todaynew);
my output like :
2018-03-20T18:30:00.000Z
I need to get date as it is but time 2018-03-20T00:00:00.000Z
When you create a new Date(), the time zone is that of the system. When you use toISOString(), the time is printed in UTC. This means that your code will print a different result when running on systems with different time zones (it prints 2018-03-20T23:00:00.000Z for me).
Instead of using setHours(), use setUTCHours().
var today = new Date(new Date().setUTCHours(0,0,0,0));
var todaynew = today.toISOString();
console.log(todaynew);
i am using moment for getting server time .
moment.tz.setDefault("Asia/Kolkata");
var now = new Date();
var _p_date = moment.tz(now, zone).format();
time when inserting _p_date = 2016-01-05T18:32:00+05:30
But in database date variable is type of DATETIME. and time is saved as 2016-01-05 18:32:00.
and after that when i comparing with this to get time_ago funcionality. providing me wrong estimation.
using time ago = moment("2016-01-05T18:32:00.000Z").fromNow(); // is showing In 5 hours
Since your initial timezone is lost you have to create moment.tz object with selected timezone. Try this plunker
var date = moment.tz(moment("2016-01-05T18:32:00.000Z", "YYYY-MM-DDTHH:mm")
.format('YYYY-MM-DD HH:mm'), 'Asia/Kolkata');
console.log(date.fromNow());
I'm trying to use moment.js to compare a date stored in the database (which is set to Europe/London timezone) against the current users time, taking into account their timezone.
I get a date string returned from the database and want to use the fromNow() function, as follows:
console.log(dbDate);
console.log(moment().format());
console.log(moment(dbDate).fromNow());
// DB stored time (Europe/London)
// 2017-09-26 06:56:26
// Current user time (timezone is Pacific Time / Los Angeles)
// 2017-09-25T23:59:03-07:00
// String output by fromNow() function, which should reflect the timezone difference but doesn't
// in 7 hours
I want the fromNow() string to take account the timezone difference and this should always be a time "ago" as opposed to in the future.
I'm probably missing something quite obvious with the library, so apologies in advance if this is very simple.
// get the current time so we know which offset to take
var now = moment.utc();
// get the zone offsets for this time, in minutes
var NewYork_tz_offset = moment.tz.zone("America/New_York").offset(now);
var MY_DATE = moment(dbDate);
// calculate the difference in hours
console.log((NewYork_tz_offset - MY_DATE) / 60);
Does this help your cause?
You have to use moment timezone, you can parse dbDate specifying "Europe/London" timezone using moment.tz:
The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.
Then you can use moment diff and fromNow.
Here a live example:
var dbDate = "2017-09-26 06:56:26";
var now = moment();
var momDbDate = moment.tz(dbDate, "Europe/London");
var pacificTime = moment("2017-09-25T23:59:03-07:00");
console.log(dbDate);
console.log(moment().format());
console.log(momDbDate.fromNow());
console.log(momDbDate.diff(now, 'hours'));
console.log(momDbDate.diff(pacificTime, 'hours'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>
I am creating a countdown for an event and the server gives me the number of seconds left till this event. It works fine in the same time zone America/New_York but I am not sure how to achieve this for a different time zone. I guess I have to add/subtract a number of seconds based on user't time zone. I am taking into account that the number of seconds returned by the server will always be in EST. Can someone advise?
So far I have this but I'm getting an error:
let serverZoneTime = new moment().tz.zone("America/New_York").offset(now);
let currentZoneTime = moment.tz.guess().offset(now);
console.log((EstTzOffset - currentTz));
First of all, if this is an event at 6pm on a certain day I would get the exact timestamp or UTC time for that event start time. Below I'm using a fake timestamp.
This is important because the people viewing your event could change from EST to DST between "now" (which you are using above) and 6pm on the event day.
It sounds like you already have the countdown working but it is just the timezone issues you are dealing with so I'll skip the countdown logic.
const moment = require('moment-timezone');
// Get User's Timezone
const userTimezone = moment.tz.guess(); // this has to be on the client not server
const serverTimezone = "America/New_York";
// Get the exact timestamp of the event date apply the server timezone to it.
const serverEventTime = moment(34534534534, 'x').tz(serverTimezone);
// Take the server time and convert it to the users time
const userEventTime = serverEventTime.clone().tz(userTimezone);
// From here you can format the time however you want
const formattedUserEventTime = userEventTime.format('YYYY-MM-DD HH:mm:ss');
// Or format to a timestamp
const userEventTimestamp = userEventTime.format('x');
For a countdown you'll want the time now as well, which follows the same logic as above:
const serverTimeNow = moment().tz(serverTimezone);
const userTimeNow = serverTimeNow.clone().tz(userTimezone);
// Get timestamp so we can do easier math with the time
const userNowTimestamp = userTimeNow.format('x');
Now all we have to do is subtract now time from the event time to get the difference and repeat every second using a setInterval() perhaps.
const millisecondsToEvent = userEventTimestamp - userNowtimestamp;
const secondsToEvent = millisecondsToEvent / 1000;
Hopefully that's useful to someone (just realized that this was two years old).
When i write data in mongodb for the recording date i use new Date() in node.js and i return that date with ajax response. To calculate the time elapsed from the moment the data in mongodb i create a new date on the client side. Then i calculate the difference between the current date and the date of which came from the server.
Here is my code:
var now = new Date();
var post_date = new Date(date_from_server);
var elapsed = now - post_date/1000;
document.write(elapsed + " seconds elapsed");
But, unknown to me, the recording date is greater than the current date, and the difference between them for the first time has a value of -40 seconds!
Please help me to understand this point. I guess that's something I'm doing wrong. But what's wrong?
It seems that the time setup between the server and client is different. You may try to emit a getFinalTime event to server, where the second time is recorded and returned to client for calculation.