Getting the offset value for a user selection with momentjs? - javascript

In my web application i have users select a certain time consisting of hours, minutes and a certain timezone.
The user selection is to trigger a function on my webserver at a certain time. But given that the user may select 11:00 in timezone "Europe/London", i need to account for the time difference between the time on my webserver and the triggering time and timezone selected by the user somehow, and calculate what the UTC version of the user selection would be.
The actual date is not important to me in this project, only that the triggering hour and minute needs to be adjusted so that a timed trigger function on my webserver matches the actual selection of time that the user has selected.
Any tips on how to achieve this using momentjs would be a great help ?

You can use moment.utc to convert all dates to UTC and then use date1.diff(date2, "hours") to get the diff in hours (or use minutes, seconds for more accuracy)

Related

How to use only one specific time zone using javascript and MongoDB

I have a web app that was created in PHP many years ago, and wanted to switch over to use Javascript and MongoDB as it's easier to scale the DB for changes over time. The dates and times are killing me, though. I've spent so much time trying to understand how it all works, and I feel like maybe I'm making things too complicated.
I have an office that is in the Eastern Time Zone. All my appointment times are set times, and the zone does not change ever. All my clients live in this time zone, and they would be scheduling by selecting an appointment time by clicking on a button associated with that time.
What do I need to do to:
save the appointment date and time to Mongo from ionic AND
display the retrieved appointment date and time from Mongo in the same time zone
I am even totally willing to just IGNORE time zone, if that is an option because I don't even care about the time zone. Daylight savings doesn't matter, because it will always be 8AM on whatever day. The time zone changing does not change the hour of the appointment.
Please help me with the most dumbed down version you have. With or without moment.js (I've tried both and keep failing miserably).
Mongo's officials documentation suggests saving timezone along with date information. Something like:
var now = new Date();
db.data.save( { date: now, offset: now.getTimezoneOffset() } );
Then you can reconstruct the original local time by applying the saved offset before you show the value:
var record = db.data.findOne();
var localNow = new Date( record.date.getTime() -
( record.offset * 60000 ) );

Generating rrules based on certain timezones

Let's say I have a user of my application set a recurring event. His timezone is America/Denver, so I store that along with an rrule that determines when the recurrences happen.
This works assuming that both my server and all of my users are in the same time zone. However, let's say I have other users, in America/Pheonix and maybe America/New_York who wants to get the occurances of this event the user has defined. I need to be able to create the events using the America/Denver time, but then return them to the user in UTC. Conversely, I also need to calculate recurring events that users in America/New_York defined and return those as UTC to the user.
Is there a library that exists that I can give it a timezone and an rrule and have it generate the recurring events based on that timezone's rules (like respecting DST)? Or maybe a third party API?
EDIT: Here is some clarity to my problem.
Let's say I have a recurring event that occurs every Friday at 9am. If I set this event in Colorado, the times that this event occurs are going to be slightly different than times in Arizona, which doesn't have DST for the 5 months of the year that Colorado does. So in my database, I need to store the time zone, and I need some way to generate the events based on that time zone's rules. This is the part I am stuck on, finding a way to generate the events based on the time zone's rules.
I need some way to generate the events based on that time zone's rules
No, you don't. The "time zone's rules" are irrelevant to the generation of occurrences. An event at "every Friday at 9am" is going to be at 9am all year round, whether DST is active or not. In other words, you can (and should) completely ignore DST for the rrule part.
The only thing you need to do is keep the original time zone along with each occurrence - a date and a time is not enough - then simply convert the date/time to another time zone on the fly, when you need to display it to a different user.
I have used Moment.js and Moment Timezone to work with dates before, but you probably have other options out there.

A Javascript based time picker control which converts the selected time into utc

I am looking for a JavaScript based time picker (jquery, extjs, bootstrap etc) control which converts the local time into UTC time. It must take into consideration user location and provide accurate time.
There are lot of controls available which allows you to pick time but I want to know what is the most accurate way to convert the time into UTC equivalent. There are lots of post available which talks about converting time into UTC but some of the post are old and some of them have one or the other catch.
What I am trying to do:
Assume the local time on user machine is 10 AM. I am asking user to pick a time > current time (or a future time). Assume user picks 11:30 AM. I want convert the 11:30 AM (local to his timezone) into UTC equivalent.

Convert set date to user's local time automatically

I am creating an application where I have a pretty big set of dates and times, and I need to display these in the user's local time and date. All set dates and times are in BST; so for example 08-24-2014 16:00 BST = 08-24-2014 11:00 EST. Now, before coming here I spent good 3-4 hours looking for an answer but if anything, I got more confused. Is there any way, to convert a set of BST dates and times to the user's local settings automatically without them setting the time zone etc?
p.s.: I have two ideas in mind but I don't know if they would work nor how to execute them.
1) Get and change the BST date and time and convert it to a unit of measurement; get and change the user's local date and time and covert it to the same unit of measurement as above, calculate the difference in the new measurement, and convert that to the user's local time.
2) Use GeoLocation to find the user's date and time/ time zone and; convert the BST to whatever the GeoLocation spits out.
you can get the users machine timezone in javascript:
var currentDate = new Date();
var currentTimeZoneOffsetInHours = currentDate.getTimezoneOffset() / 60;
see documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
having the offset you can add it to your values
there is a javascript library Moment.js which allows you to query daylight saving values for specific dates and timezones.
because all your dates are in british summer time. first query if a date is in british winter time with isDSTShifted(). and subtract() an hour.
convert the date to another timezone

javascript timezone converter

I need a way to convert times in the future to different timezones without relying on the user's computer time.
At registration time, the user supplies his timezone. When he logs in, I calculate the offset in minutes between the UTC time and his time and inject that offset into the page so that a javascript function can do the conversions. Something like this:
var TheUTCTime = new Date(UserTime.getTime() - TimeZoneOffsetInMinutes * 60000);
and like this for the other way around:
var TheUserTime = new Date(UTCTime.getTime() + TimeZoneOffsetInMinutes * 60000);
This works really well to convert times as long as the offset doesn't change. For instance, because of daylight saving, between US EST and UTC, there's a difference of 300 minutes or 360 minutes depending on the month in the year.
My functions work well to convert today's date but I'd like something that can 1) do the same thing for any day of the year and 2) doesn't depend on the user's internal clock or timezone.
How could I do this?
Thanks.
My functions work well to convert today's date but I'd like something that can 1) do the same thing for any day of the year and 2) doesn't depend on the user's internal clock or timezone.
If you want to convert another UTC time into the user's local time, you have to know their time zone. That's pretty much the definition of a time zone: a mapping between UTC and local time (or equivalently, between UTC and the offset from local time).
As you've seen, getting the current offset isn't enough, because of daylight saving transitions (and any other changes to time zones - they vary more than you might expect).
Basically there's no way round this: you will have to ask the user for their time zone. You can make a good guess based on the current offset from UTC and possibly geocoding their IP address, but you'll have to confirm it with them. (For example, they may be on a trip or something, and not in their "home" time zone.)
To don't depend on the user's clock timezone, I think the best approach is to do conversions in the server side.
There's a good question here in the SO that covers daylight saving time and javascript Date object: Daylight saving time and time zone best practices

Categories

Resources