Convert date to utc using the timezone passed from client(javascript)

javascript display date in different time zone
add timezone offset to date javascript
javascript convert utc to specific timezone
javascript set timezone globally
javascript get timezone offset for specific timezone
convert local time to est in javascript
javascript change timezone on date
convert utc to local time javascript

I have a logic to show a message on the user screen for a particular time duration mentioned by the admin.

var time = DateTime.SpecifyKind(System.DateTime.Now, DateTimeKind.Unspecified);
var date= TimeZoneInfo.ConvertTimeToUtc(time, TimeZoneInfo.FindSystemTimeZoneById(System.TimeZoneInfo.Local.Id));
StringBuilder dispalyMessage = new StringBuilder("<marquee scrolldelay='90' behavior='scroll' scrollamount='1' direction='left'><b><ul>");
var bannerMessageIds = new HashSet<int>(db.CompanyBanners.Where(c => c.CompanyId == PortalUser.Current.CurrentCompanyId).Select(x => x.BannerMessageId));
List<BannerMessage> bannerMessages = db.BannerMessages.Where(p => bannerMessageIds.Contains(p.Id)  && (p.StartDate < date && p.EndDate > date) ).ToList();

Here the problem is I am converting the time to check using the server timezone.

Is there a way to pass the client timezone from javascript to c# and convert the date as per that time zone.

I mean something like this

var date= TimeZoneInfo.ConvertTimeToUtc(time, TimeZoneInfo.FindSystemTimeZoneById("time zone id passed from client javascript"));

I think NO. but you can get the timezoneoffset in JavaScript like Below.

function myFunction() {
var d = new Date();
var n = d.getTimezoneOffset();
document.getElementById("demo").innerHTML = n;
}

This should serve your purpose.

How to convert a JavaScript date to UTC?, (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed. Here the problem is I am converting the time to check using the server timezone. Is there a way to pass the client timezone from javascript to c# and convert the date as per that time zone. I mean something like this. var date= TimeZoneInfo.ConvertTimeToUtc(time, TimeZoneInfo.FindSystemTimeZoneById("time zone id passed from client javascript"));

Client-side

  Create a script that will be executed at every time the page loads.
    In the script check if the cookie that stores timezone offset exists.
    If the cookie not exists check whether the browser supports cookie.
    If the browser supports cookie create a new one and store the timezone offset.
    Reload the page if the cookie is created for the first time or expired.
    If the timezone cookie already exists check if the current timezone and the one stored in cookie are same. 
If they are different then overwrite with new timezone and reload the page.

Server-side

    Read the cookie value on every request and store it in session.
    Create a utility method that converts the passed datetime in UTC to the client timezone by adjusting it with the offset read from session.
  If there is no value in session possibly the browser doesn’t supports cookie then convert the date-time to server timezone.

Look here for complete implantation and more hints

What time zone does the JavaScript new Date() use?, Automatic timezone conversion in JavaScript is not exactly fun. Keep reading to find out how we deal with one of JavaScript's most common However, it turns out that the JavaScript Date object does its own client-side timezone conversion based This only works when the timestamp passed is in GMT. Date and time are a regular part of our everyday lives and therefore feature prominently in computer programming. In JavaScript, you might have to create a website with a calendar, a train schedule, or an interface to set up appointments. These applications need to show relevant times based on the user’s current timezone, or perform

If you are storing the datetimes in the database in UTC, you shouldn't care. So unless I've missed something (always possible :)), can you not just query on the value of DateTime.UtcNow, and not do any timezone conversion at this part?

e.g. User's timezone, it's 13:00. Server's timezone, it's 10:00. UTC time is 09:00.

When you store the times in the database, you convert the entered dates/times from the user's timezone to UTC (either in javascript or server-side), so a user-entered time of 13:00 gets stored in UTC as 09:00.

When the user then hits the page, you want to find all messages that should be shown "now". "Now" to the user is 13:00, but in UTC it's 09:00. So searching based on that UTC time should be all that's needed.

Date getTimezoneOffset() Method, Convert the local time to another time zone with this JavaScript. The first order of business is to obtain the current local time. Next, find the local time zone offset with the Date() object's getTimezoneOffset() method. Obtain the current UTC time, by adding the local time zone offset to the local time. Do you feel the need for a library even for a small task like converting local Date to UTC format? It is really easy to convert local time to UTC in Javascript. constructor and pass the

GMT UTC in United States, I pretty well knew that the time zone support in JavaScript is… In some cases, a country can change not only its DST but also its standard time. For example, Samoa used to use the UTC-10:00 offset, but later changed to the However for this to work, the date and time transferred from the client to the server must be  Let’s meet a new built-in object: Date. It stores the date, time and provides methods for date/time management. For instance, we can use it to store creation/modification times, to measure time, or just to print out the current date. To create a new Date object call new Date () with one of the following arguments:

Automatic timezone conversion in JavaScript, Well Javascript Date constructor is actually helpful and also easy! Do you feel the need for a library even for a small task like converting local Date to UTC format? with dates can be done with the native Date constructor given by Javascript. So let's You would not want to store date in local timezone when your server is  Javascript has a getTimezoneOffset method which gives timezone difference, in minutes, from current local time to UTC. We can convert this into hours and minutes, and get the timezone offset in hours and minutes. Note that getTimezoneOffset returns an offset which is positive if the local timezone is behind UTC and negative if it is ahead.

Convert the local time to another time zone with this JavaScript , How to pass an array as a function parameter in JavaScript ? This will format the date to the timezone required and return a string with the How to convert a JavaScript date to UTC? How to get the client timezone offset in JavaScript? In the realm of software development, time and date manipulation is considered by many to be a difficult task. Complicated time zone rules, leap seconds, differences in locale-specific formatting, etc., force us to immediately resort to popular time and date manipulation libraries. We often use these libraries witho

Comments
  • Your database should store time in UTC, and so you should compare against DateTime.UtcNow, without any timezone conversions.
  • Yes. I know. But is there a way to get the client timezone on server side? I know its not a direct question.