user1493460
user1493460

Reputation: 71

ASP.NET match users local date and time with the system time

I have some trouble understanding how to match users current date and time with the server time.

Example: let's assume I have a website where users can register themselves. One profile option is that they can select their local time zone. For this there is a drop-down menu from where they can choose the correct time zone. So a user from China will select probably (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi, another user from Los Angeles will select (UTC-07:00) Mountain Time (US & Canada) (i assume) and a guy from Paris will select (UTC+01:00) Brussels, Copenhagen, Madrid, Paris.

The web application is running on a server in USA with it's specific time zone ...

Now ... All these users will want to receive an email notification next Friday at 19:00 their local time zone!!!

Here I am lost ... definitely the next Friday at 19:00 is not in the same time for all these users ...

How can I map their profile time zone, so the service running on my site will send the email notification next Friday at 19:00 user's local time zone???

I am at this stage at the moment ... populating the drop down menu with all time zones so users can select their current time zone in their profile.

When the page load's than the dropdown is populated with the time zone:

    protected void Page_Load(object sender, EventArgs e)
    {
        ddlUserTimeZone.DataSource = GetTimeZones();
        ddlUserTimeZone.DataTextField = "Name";
        ddlUserTimeZone.DataValueField = "ID";
        ddlUserTimeZone.DataBind(); 
    }

    public Collection<TimeZoneItem> GetTimeZones()
    {
        Collection<TimeZoneItem> timeZones = new Collection<TimeZoneItem>();
        foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
        {
            timeZones.Add(new TimeZoneItem 
            { 
                TimeZoneName = timeZoneInfo.DisplayName, 
                TimeZoneID = timeZoneInfo.Id 
            });

        }

        return timeZones;
    }

    public struct TimeZoneItem
    {
        public string TimeZoneName { get; set; }
        public string TimeZoneID { get; set; }
    }

Now, can you guys help with the matching of the profile time zone with the current time so the email is sent in the correct time?

Thanks in advance!

Upvotes: 1

Views: 1969

Answers (2)

Amith George
Amith George

Reputation: 5916

DateTime should ideally be stored on the server in UTC format.

You have the following data on your server

  • Timezone info of the user.
  • The time at which the user needs the notification.
  • The current local time on your server.

    // Convert current local server time to UTC.
    var serverUtc = DateTime.UtcNow;
    
    // Convert UTC time to users local time. This gives you the date and time as per the user.
    var userTimeZone = TimeZoneInfo.GetSystemTimeZones()[0]; // just an example. Replace with actual value.
    var userCurrentTime = TimeZoneInfo.ConvertTime(serverUtc, userTimeZone);
    
    /*
    add a day to userCurrentTime till its Friday. Add/subtract minutes till its 7:00PM.
    */
    
    var expectedNotificationTimeUtc = TimeZoneInfo.ConvertTimeToUtc(userCurrentTime, userTimeZone);
    /*
    1. store the expectedNotificationTimeUtc as the time you want to send the email. 
    2. your service can check for users their expectedNotificationTimeUtc and 
         if the UtcNow is within an acceptable range of the that time, send the email.
    */
    

Upvotes: 0

O. Jones
O. Jones

Reputation: 108706

Are you just setting up this service? If so, run your web servers and database servers on Universal Time Coordinated (UTC or Zulu) time, not a local time zone. Everything is far easier to manage if you do that. I learned this the hard way.

UTC used to be called Greenwich Mean Time; it is timezone +00:00. It doesn't change for daylight savings time like US and European local time does.

This time zone thing is a pain, and worth getting right. Some countries have half-hour time zones.

At any rate, once you know each user's preferred time zone, and the time she wants her notification, you can then convert the time from local to UTC and store it.

Try something like this to get the user's hour and minute into UTC. (Time zone conversions need a date, because they depend on daylight savings time rules. There's one more complication. On the day when a time zone switches from daylight to standard, or vice versa, the UTC time for a notification will change. Most people handle this by recomputing the UTC date and time of the next notification (tomorrow's notification) as they send each notification. Consider this code.

TimeZoneInfo userLocal =            ;//user's time zone
int hour =                          ;//whatever the user wants
int minute =                        ;//whatever the user wants

DateTime tomorrow = DateTime.Now.AddDays(1);
int year = tomorrow.Year;
int month = tomorrow.Month;
int day = tomorrow.Day;
DateTime notificationTime = new DateTime(year,month,day,
                                         hour,minute,0, 
                                         DateTimeKind.Unspecified);
DateTime tomorrowNotificationTime = TimeZoneInfo.ConvertTimeToUtc(
                                         notificationTime,userLocal);

This should get you the UTC time you need to deliver this user's notification tomorrow, in the correct time zone for tomorrow's date.

Upvotes: 2

Related Questions