calculate now time until specific time javascript - javascript

i have a project with reactjs and in this project I want create a countdown and in my project i want when ever a user came to my site I showing that user a count down from The moment the user enters the page until 00:00:00 (12:00:00 AM). i try implement this with javaScript and Date class but I can't find a solution for this problem.in other word i want a time stamp from the moment user enters the page until (00:00:00)
(ex:a user enter the page at 11:00:00 i want this timestamp be between 11:00:00AM and 00:00:00)

I would suggest moment.js package. It's very well documented and it can do pretty cool stuff. I'm sure it would solve your need. Here is a link to moment.js

Related

Moment.js local time zone issue

Using the code below, I am getting a Date from my database (SQL) and displaying it in a datapicker form field. The date displays fine for me but if I change my time zone (EST) in my system to one that is behind mine, the field will display the date as the day before. Does anyone know why this is occurring and how to fix it?
var NetNewBusinessDate = moment(model.NetNewBusinessDate).format("M/D/YYYY"));
model.NetNewBusinessDate == "/Date(1494561600000)/"
Found the answer here. Moment.js local relative time
The issue had to do with UTC conversion and Moment.js has an extension method that handles that. I just used moment.utc instead of moment and it worked like a charm!

Managing timezone & DST issue for javascript application

I am trying to create a scheduling application. The front end\UI is developed using JavaScript. The back end is a ASP.NET Web Api application which uses MSSQL server as the database. From the UI, user will schedule a job which can run daily/weekly/monthly. Each job can run for maximum of 3 months. The job will run on the server side at the specified time.
Assume user come and selects a job which will run for a week (From 23-Nov to 29-Nov) at 10 AM local time. In this case, I will make seven entries in the database starting from 23 nov (One for each day). Each row will have Start time, Start Date and some status related columns.
I have following querstions:
How do I store time information (10 AM in this case)on SQL server?
Should I get the time using JavaScript on client machine and then convert the same to UTC?
Should I get the time using JavaScript and also save the user time zone information?
What happens when DST related changes take effect?
Will library like momemnt.js will help in this scenario?
I am thinking of saving user timezone information and the saving his local time on the server.
Warning - Scheduling properly is hard. There's a lot more to consider. Please read this and this. Most of your questions are addressed there (though from the perspective of other languages, the challenges are the same).
You might also take a look at Quartz.net, which is sufficient for many scenarios.
To answer your specific questions:
How do I store time information (10 AM in this case) on SQL server?
For the recurrence rule, store the local time of the event. SQL Server has a time type, which works well for storing the time of day. You will need other fields for tracking the time zone, the start date, days of the week, and other pattern information.
For the specific instance that is scheduled to run, you calculate the UTC datetime based on all the information in the recurrence rule. At minimum, you schedule the next occurrence, and recalculate after each run. In some cases, you may decide to project the next N occurrences, depending on what you need to show to the users. (You could also use a datetimeoffset for this purpose. See datetime vs datetimeoffset.)
Should I get the time using JavaScript on client machine and then convert the same to UTC?
Should I get the time using JavaScript and also save the user time zone information?
To answer both questions: For scheduling, you should not discard the original input, which will be in the local time zone of the event being scheduled. That may or may not match the time zone of the user. You will need to ask the user to select the time zone of the event.
What happens when DST related changes take effect?
That's up to you. You will need to test this thoroughly. In general, there is a period of local time that is skipped, and a period of local time that is repeated.
When it is skipped, you have to decide when to run the event. Options include: 1) before the skipped time, 2) after the skipped time, and 3) not at all. In most cases, the preferred option is to run after the skipped time, by advancing the local time by the DST bias (usually 1 hour). For example, a daily event scheduled to run at 2:30 every day in Pacific time would run at 3:30 on the day of the spring-forward transition.
When it is repeated, you have to decide when to run the event. Options include: 1) at the first occurrence, 2) at the second occurrence, and 3) at both occurrences. In most cases, the preferred option is to run at the first occurrence only. For example, a daily event scheduled to run at 1:30 every day in Pacific time would run at 1:30 PDT, and not at 1:30 PST.
Exceptions to this include dealing with businesses that are open late into the evening and choose to stay open for the repeated hour. For example, a bar, restaurant, or movie theater. It is highly dependent on the specific use case and the choices made by the specific business.
Will library like moment.js will help in this scenario?
Not from a scheduling perspective, no. It can help with parsing, formatting, and validating input though. You might also use moment-timezone to help with selecting the event's time zone. If you were running this with node.js on the back end, then perhaps there would be more benefit.
The biggest challenge is actually one you have not talked about, which is maintaining the time zone data on your server. In your C# code, I recommend using Noda Time for this, instead of TimeZoneInfo. You can then update the tzdb data yourself as needed. You also need to think about the workflow of rescheduling the UTC instants of each occurrence, in the case that a time zone has changed its offset or daylight saving time dates.

getting time zone when entering state and country (time zone to use in ics file)

Getting time zone when giving state and country in text box
is there any way to get the time zone of that place with this two values?
the input will be like
var state = 'New York' ;
var country = 'United States';
result should be
America/New_York
OR
get local time zone of the current browser?
I need this time zone to use in ics file.
This is impossible, for the simple reason that quite a few states span more than one timezone. See Wikipedia's list. Outside the US it can get even more complicated; I believe there are some cities that span multiple timezones.
You could cobble together guesses per state (e.g. by using that list), but if this is for figuring out the user's timezone, you'll probably have better luck just comparing the client clock with the server's UTC time and estimating based on country.
edit: Note that there's no way to ask the browser for the current timezone, either, and you can't guess reliably based on the current time, because there are many timezones where it's the same time right now but where DST is different. Your best bet is to find all the possible current timezones, estimate based on the user's location (which you also have to guess!), and just ask as a last resort.
No, there is no pre-defined methods exists.
For this you need some external web services. If you're interested you can create your own API using the information provided here in wikipedia
Updates: Based on your comments "get local time zone of the current browser
var date = new Date();
returns 12:38:05 GMT+0530 (India Standard Time)
To pick the time within the bracket use
date.toTimeString().match(/\(([^)]+)\)/)[1];
returns India Standard Time
Check this JSFiddle
But this is not you expected, however you should try Auto detect a time zone with JavaScript and for updated version try this jsTimezoneDetect
GeoNames provides a data dump that you can use:
http://download.geonames.org/export/dump/
This is per skylarsutton's response in the following previous post with a similar question (but not specific to js or query)...
I need a mapping list of cities to timezones- best way to get it?

Javascript Timezone Change

I made small JS app using Sencha Touch/ExtJS. It display some date values taken from database. But those date get changed depending on client's time zone.
How can i stop automatic time and date change and display values as i enter ?
use moment.js, it handles all kind of timezone issues.

time zones: user preference vs client-side Javascript

In Javascript it's fairly straightforward to render and manipulate dates in the current user's local time zone using the methods of the Date object. For example, toLocaleString() for output and the 7-argument form of the Date constructor for input. So up until now I haven't even bothered setting a time zone as a user preference. Internally everything is stored, computed and sent back and forth to the client using UTC, and we do the translation client-side for both input and output.
For example, suppose a user has their local machine's time zone set to US Eastern. When looking at my web page, an event that occurred at Unix timestamp 1359416775000 would render as, say "Mon Jan 28 18:46:15 2013" with code no more complex than
new Date(1359416775000).toLocaleString();
But suppose I need to send that user an email about this event. What time zone should I use to render the timestamp in this email? The obvious answer is to let the user pick their time zone. Now suppose I do this and this user picks US/Eastern. Great. Now suppose the next time the user logs into my website their local machine is on US Central time. That same piece of Javascript code would now cause the timestamp to render as "Mon Jan 28 17:46:15 2013".
Is this really the right behavior? The user has picked a time zone in my application, but it only applies to email?
This seems like a common enough problem that I feel like there ought to be a common best practice, I'm just wondering what that is.
You should, by default always display times in the users local time zone. At any point of you display the time using another time zone, this should be made clear by also printing the timezone.
As such, if the users time zone is US/Eastern, you would display a time in hos time zone, in your example "Mon Jan 28 18:46:15 2013", while if you show him an event that actually happens in US/Central, you should show "Mon Jan 28 17:46:15 2013 US/Central".
Now if the user moves to a computer whose time zone is US/Central, then yes, as default you should now show him the time in US/Central. So you would in both cases display the date as "Mon Jan 28 18:46:15 2013", no time zone necessary. They will have the computers current time in the corner of the screen, so it won't cause much confusion.
If you let the user pick his timezone, which is common in sites where the time display isn't decided by the client time zone setting, then you should by default show the times in that time zone all the time, no matter what time zone the computer is in. Remember that it is up to the user to make sure his computer is in the right time zone. Most people who travel with their laptops won't change the time zone when they move.
It is theoretically possible for you to warn the user that he has selected another time zone than the one he seems to be located in, by getting a geolocation from the IP. But unless you are writing a calendar application I would think that annoys people more than it helps them.
Unfortunately you cannot set the user's timezone which is used by the non-UTC date methods.
You can only work around that by adding/substracting your custom timezone offset when outputting/reading a date, like in this example.

Categories

Resources