Calling TimeZone Then Running Search With It - javascript

I have a user select a timezone. I then need to get the timezone that they have selected and have it passed through a search not the timezone the computer is using.
I know that:
.getTimezoneOffset //returns the computers offset
I need to get the timezone that the user has chosen as his or her timezone and pass it through the search.
My code looks like this
var expireFrom = $('input[name=expireFrom]').datepicker('getDate');
var expireDate = new Date();
if(expireFrom!=null)
expireFrom=expireDate.getTimezoneOffset();
How do I call the users chosen timezone and not the computers?

The Javascript Date object is not capable of working with other time zones directly.
Time Zone != Offset
Date pickers don't typically have time zone selection. If you have one that does, you need to tell what you're using.
If you really want to let your users pick a time zone, you'll need a control like this one.
To do anything useful with time zones, you'll need a TZDB implementation. You are best doing this in your back-end server-side code. If you must do it in JavaScript, you can try one of the libraries mentioned in this answer.

Related

How do I get the result of javascript new Date().getTimezoneOffset() transferred to C# backend?

See the title: for the solution I'm working on, I need to get the current timezone offset (from the client, running javascript/jQuery) and use it in backend C# code.
The question is rather similar to the one asked here, but there are a few differences - the main one being that I am pretty sure that the time on the client computer won't be tampered with. So new Date().getTimezoneOffset() will do just fine.
I cannot read the value upon submitting a form since the user is not working in a form: after the user has logged in, among the items that are visible on the screen is a table with data entered by the user or by other users. This data contains UTC datetimes that have to be adjusted according to the client's timezone. C# code is responsible for retrieving and formatting the data - hence my question.
What would suffice, is storing the value somewhere so that C# can read it when necessary. But I don't think that can be done as well. What would be the approach here?
Thanks in advance!
Your suggested approach is flawed in that the current offset from the client's browser is only going to apply to the current date and time. In reality, time zone offsets change over time within a given time zone. You cannot just take a singular offset from one point in time and expect to use it to convert other dates and times to the same time zone. Instead, you need to use the string that identifies the time zone, not an offset from that zone.
As an example, consider the Eastern time zone in the United States. For part of the year, it uses UTC-5, and we call it Eastern Standard Time (EST). In another other part of the year, it uses UTC-4, and we call it Eastern Daylight Time (EDT). This time zone is identified by either the IANA time zone ID "America/New_York", or the Windows time zone ID "Eastern Standard Time" (which covers the entire zone, both EST and EDT despite its wording).
So, break this problem apart into a few steps:
In JavaScript, identify the users's IANA time zone (America/New_York):
If you are targeting modern web browsers, you can call this function:
Intl.DateTimeFormat().resolvedOptions().timeZone
If you need to support older web browsers, you can use jsTimeZoneDetect, or moment.tz.guess() from Moment-Timezone.
Send that string to your web server through whatever mechinsm you like (form post, XHR, fetch, etc.)
In your .NET code, receive that string and use it to reference the time zone and do the conversion. You have two options:
You can use Noda Time, passing the IANA time zone ID to DateTimeZoneProviders.Tzdb as shown in the example on the home page.
You can use .NET's built-in TimeZoneInfo object. If you're running .NET Core on non-Windows systems (Linux, OSX, etc.) you can just pass the IANA time zone ID to TimeZoneInfo.FindSystemTimeZoneById. If you are on Windows, you'll need to first convert it to a Windows time zone ID ("Eastern Standard Time"). You can use TZConvert.GetTimeZoneInfo from my TimeZoneConverter library.
Once you have either a DateTimeZone from Noda Time, or a TimeZoneInfo object, you can use the methods on it to convert UTC values to local time values for that time zone. Each of these will apply the correct offset for the point in time being converted.
I'll also say, many applications simply ask the user to choose their time zone from a dropdown list and save it in a user profile. As long as you're storing a time zone identifier string and not just a numeric offset, this approach is perfectly acceptable to replace steps 1 and 2 above.

Using moment.js, can my date or my date timezone change if I use a VPN?

I'm new with javascript and I am using moment.js.
My code is const date = moment().
Let's supposed one of my website's user (A) is using a VPN, locating him to an area using a different timezone. The other user (B) is using the website in the same country but with no VPN.
Will the value of date be different for the two users, and will it's timezone be the same?
Thanks in advance!
Not necessarily, although it has nothing to do with the IP address/VPN usage.
The date, time and timezone that new Date() will give is entirely dependant on the host OS configuration. There's even no way you can guarantee that 2 machines on the same network have the same/different time or date.

Does getTimezoneOffset() contain Day Light Saving information and how do I get these both values?

Date stored in server at GMT+0 without Day Light Saving.
Client should receive it in it's own timezone format. So we can get time zone offset and summarize it with received date:
const currentTime = new Date();
this.timezoneOffset = currentTime.getTimezoneOffset();
Then we can check if client in DLS region somehow, for example using momentjs:
const isDaylightSavingTime = moment().isDST();
And there I can't figure out 3 things:
1) does getTimezoneOffset() already contain Day Light Saving information
2) and if not, what the better way to check is user in DST then moment (because I readed thet moment js have not some countries and cities in it's own base)
3) does Moment().add(Moment().utcOffset(), 'm') return date with Day Light Saving?
And there I can't figure out 3 things:
1) does getTimezoneOffset() already contain Day Light Saving information
Moment is a wrapper for ECMAScript Date objects. The timezone offset for a built–in Date instance is based on host system settings. Depending on the implementation, it may or may not reflect historic changes to timezones for the related date (i.e. the Date that the method is called on). It will at least reflect the current settings.
2) and if not, what the better way to check is user in DST then moment (because I readed thet moment js have not some countries and cities in it's own base)
You need to define "better". In addition to other libraries (e.g. Luxon), there are web APIs like timezonedb and Date.prototype.toLocaleString with the timeZone option that uses IANA location names like "Asia/Shanghai". You can also download and use the IANA timezone database if you wish.
3) does Moment().add(Moment().utcOffset(), 'm') return date with Day Light Saving?
You can't modify the timezone offset, there is only a getter, so any "local" date will have the timezone offset applied, whether that be the standard or daylight saving offset for the particular date and location. All the above does is shift the time by the offset, it doesn't change the timezone (unless it's shifted across a daylight saving boundary).
Date objects are UTC at heart, so the general approach is to use UTC for everything and only consider the timezone offset for presentation. That doesn't cover all scenarios, but it does the majority. The remaining cases require specific approaches based on use cases.
If you describe what you are trying to do, you might get more relevant answers.

Moment js utc() time in london - BST

I'm using momentjs lib to updated text on some ajax action. What I need to do is to set a current date & time in london. I'm using moment.utc() function but because of the summer time I'm one hour out.
For example running this on 14:26
console.log( moment.utc().format('HH:mm:ss') );
I'm getting 13:26:53.
Any idea on how to fix this?
Can you use momentJS timezone?
moment().tz('Europe/London');
EDIT: In case you try to use this without seeing the link, it's a separate library you have to include.
If you want the local time instead of the UTC time, just use moment() instead of moment.utc(). You're specifically asking for UTC, so you shouldn't be surprised when you get UTC :)
From the documentation:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.
This is assuming you always want the user's local time. If you want a specific time zone (London) which may not be the user's time zone and isn't UTC, then you should use the library indicated by Takuya's answer. I would think carefully before doing so though - while it may be a sensible approach, you should at least validate that first. It's often reasonable to display a time for user U1 in the time zone of user U2 - but here you're using a fixed time zone. That's only appropriate if you know that U2 will always be in London. It would be really confusing if actually U2 is in some other zone - either the same as or different to U1.

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?

Categories

Resources