Time zone offset showing incorrect date and time - javascript

I am trying to show time based on user local time zone. Server saves time to utc.
So, I have a date-time saved in my database.
2018-10-03 05:55:51 // my server is digital ocean
So now I am trying to console user local time. My time zone is set to
Sylhet Bangladesh
Time offset is -360
var offset = new Date().getTimezoneOffset();
console.log(offset) // offset is -360
var testDateUtc = moment.utc("2018-10-03 05:55:51");
var localDate = moment(testDateUtc).utcOffset(offset);
console.log(localDate.format("YYYY-DD-MM hh:mm:ss"));
The above code prints incorrect date but correct time.
2018-02-10 11:55:51 the date is wrong.
I then changed my mac's time zone to Dubai which is 2 hours different then my country
For dubai the offset is -240 and it shows time
2018-03-10 01:55:51 this mean the date is correct but time is not correct.
Please help. Thank you.
EDIT
It works for most of the countries I tested like this
offset = Math.abs(offset) so it always make it positive

You do not need to think about the offset. Moment can do that for you:
moment.utc("2018-10-03 05:55:51").local().format()
Also, keep in mind that when you asked for the offset in your code, you asked for the current offset, which may or may not be the offset in effect at the time you're converting. See "Offset != Time Zone" in the timezone tag wiki.

Related

Momentjs how to get timezone time - not based on computer datetime

I need to get the time of a time zone but not based on the computer time,I don't want the time to change when the computer or phone time changes.
I need to get the time with a zone
I have tried lot of examples available in stackoverflow and from other but is hard to find it
moment.tz.setDefault("Europe/London");
var datetime = new Date(moment());
document.getElementById('log').innerHTML =datetime;
above code prints the time. When i manually change the time of my phone it's shows the changed phone time not the actual zone time.
Let say am in London and my local time 14:30 so my zone time also 14:30 when I change my phone time to 15:30 it should show actual zone time 14:30 not 15:30
Firstly is that possible? if yes please help
const datetime = moment().tz("Europe/London").format('h:mm:ss a');
// change format as needed…
console.log(datetime);
document.getElementById('log').innerHTML = datetime;
Make sure you have the necessary resources loaded.
Fiddle here: https://jsfiddle.net/dusthaines/hg1fpwue/

How to display Datetime considering offset using MomentJS with Timezones

I'm using moment.js with timezones to create a datetime belonging to a specific timezone:
var datetime = moment.tz("2016-08-16 21:51:28","Europe/London");
Because this constructor is aware of DST (daylight saving time), moment.js will add +1 hour offset automatically. datetime.format() will show: 2016-08-16T21:51:28+01:00.
But it seems when printing the date, the offset ins't considered. E.g. datetime.format('DD.MM.YYYY - HH:mm:ss') will show: 16.08.2016 - 21:51:28 but I wan't it to show: 16.08.2016 - 22:51:28 (the time considering the DST-Offset of 1 hour). Does anyone know how to do this?
You are misinterpreting the output you're getting.
When you see +01:00 at the end of an ISO8601 timestamp, it doesn't mean that you need to add an hour. It means that timestamp given is in a local time zone that is one hour ahead of UTC at that point in time. Moment isn't adding an hour. It's simply reflecting the local time in London.
For the timestamps you provided, showing 22:51:28 would be an error. The local time in London is 21:51:28, and the equivalent UTC time is 20:51:28. You wouldn't find 22:51:28 until you went one time zone to the East, at UTC+2.
Now, if what you meant to do is convert from UTC to London time, then you need to create the input as UTC and then convert.
moment.utc("2016-08-16 21:51:28").tz("Europe/London")
Then you'd get 22:51:28 when formatting, which is the result you asked for, but this is a different point in time.

how do i use javascript change timezone of date without changing the time

I have users who's local timezone can be different from a facility they are setting up event start times for. I want to take the time (say 10 am) and change the javascript date object to be 10am in the facility timezone and not the local timezones. I do know the utc offset of both local and desired timezone. I have not been able to link it all together. Here is my example.
var localOffset = 420;
var desiredOffset = 360;
var eventStartDate = moment(localStartDate);
eventStartDate.subtract(localOffset - desiredOffset);
my original time is in MDT and my desired is CDT but the start time of 10am needs to remain the same. If I can do this without using momentjs that is fine.
Take a look at the documentation for the subtract method:
http://momentjs.com/docs/#/manipulating/subtract/
You're missing a unit in your call. It appears that you're using minutes, so your call should be something like:
eventStartDate.subtract(localOffset - desiredOffset, 'minutes');

Momentjs Grabbing today's date and setting time causes it to fast-forward 24 hours

I'm trying to time out email messages based on user preferences. My morning calculations are working correctly but it's the evening emails that are never getting sent because dates aren't behaving as expected.
First, here's the code I'm using to grab the time and perform adjustments based on user location, etc.
var time = moment();
var machineTZ = time.zone();
var userTZ = 420;
var diffTZ = userTZ - machineTZ;
var oneHour = moment(time).add('minutes', 60);
var morningRun = moment().startOf('day');
morningRun.hour(7).minute(0);
morningRun.add('minutes', diffTZ);
var eveningRun = moment().startOf('day');
eveningRun.hour(19).minute(30);
eveningRun.add('minutes', diffTZ);
I'm checking every hour to see if it's time to schedule another email to go out. Right now this is hard-coded, but when I begin to add user preferences they'll be able to select their local time they'd like things to go out at.
I've been debugging my values to troubleshoot. Here's output from a job that ran early morning (from the server's perspective):
lastRun: 2013-10-12T00:06:55.088Z (this one is being run at 1 am)
morningRun: 2013-10-11T14:00:00.000Z
eveningRun: 2013-10-12T02:30:00.000Z
The run numbers are as I would expect them to be. In two hours time I want the evening email to go out (7:30pm my time = 2:30am the following day server-time).
Looking again an hour later we see:
lastRun: 2013-10-12T01:06:58.267Z (this one is at 2 am)
morningRun: 2013-10-12T14:00:00.000Z
eveningRun: 2013-10-13T02:30:00.000Z <---- what?
All of a sudden my calculation for my evening has flipped over the date line, even though it's still 10/12 (not 10/13 yet). Because of this my check to see if I should schedule the email fails since it now thinks I need to send the email in 24 hours, not 30 minutes.
Been battling with this weird inconsistency for a while, I thought I had figured out why it was doing it and adjusted my calculations using the time zone stuff above but that didn't do the trick :(. This certainly seems like some sort of weird bug as I would expect this to be happening:
//Today is 10/12
var eveningRun = moment().startOf('day'); //10/12/2013 - 00:00:00
eveningRun.hour(19).minute(30); //10/12/2013 - 19:30
eveningRun.add('minutes', diffTZ); //10/13/2013 - 2:30 am
This works until at some point it decides that "today" is actually 10/13 and sets the evening run is to take place on 10/14 instead. Help is greatly appreciated, or if this is a bug would love to know what I can do to work around this issue until it's resolved.
There's no need to calculate machineTZ, diffTZ or add any minutes. Just do this instead:
moment().zone(userTZ).startOf('day').hour(7).minute(0)
But do keep in mind that a value such as 420 is not a time zone, it's a time zone offset. It only tells you what the offset is for a particular point in time. Since you are applying it unilaterally, you may get incorrect results during daylight saving time.
Instead, you should try using the moment-timezone addon, and do something like this instead:
moment().tz("America/Los_Angeles").startOf('day').hour(7).minute(0)
See also the timezone tag wiki, in particular the sections titled "Time Zone != Offset" and "The IANA/Olson Time Zone Database".

Timezone offset of any time using using JavaScript Date

I am using var offset = new Date().getTimezoneOffset() to get the current timezone offset of user.
But, how can I get the timezone offset for any future time?
This is required because the timezone offset is different when DST is enabled/disabled.So I cant assume the same offset for future time.
This question confused me into thinking that perhaps getTimezoneOffset() wouldn't correctly get the timezone offset for future dates already. It does.
As far as JavaScript is concerned, you give it a milliseconds since the epoch date, which is ubiquitous, and it gives you the offset in local time as it will be on that date, because all the Date functionality cares about is taking a universal date/time and passing back a date that will make sense to the local user, unless you specifically ask it for the universal equivalent, with methods like getUTCDate().
So running
var summer = new Date(1403170847000);
alert('Summer offset=' + summer.getTimezoneOffset());
var winter = new Date(1419500175000);
alert('Winter offset=' + winter.getTimezoneOffset());
in a console where the local time includes a DST period, reports for me in the UK an offset of -60 (i.e. GMT+1) for the date in the summer, and 0 (i.e. GMT) for the date in the winter.
http://jsfiddle.net/SX9SN/

Categories