Solved

timezone headache - utc +10 as UTC

Posted on 2014-03-03
4
284 Views
Last Modified: 2014-03-04
I have all my data stored in utc format
some users have their user account set to different time zones for example UTC + 10, so they need to filter data within their time zone days

for example, i want to filter a days data ( the 28th Feb)
according to the attached, if i want to select the days data for brisbane, its UTC time is 14:00 to 13:59 the next day

however, this code is always selecting 10:00am UTC
 public static String GetUsersLocalDateTime(DateTime utcTime, string timeZone)
        {
//            DateTime userTimeZoneDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(Convert.ToDateTime(utcTime.ToString("yyyy-MM-dd HH:mm:ss")), timeZone);
         

Open in new window


of course this is just doing a UTC +10 conversion on time

but really the UTC time for midnight in brisbane is 14:00 gmt/utc

what code do i need to get the 24 hour period for brisbane
Book1.xlsx
0
Comment
Question by:websss
4 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39902681
I don't develop in c# and only answering because of the neglected alert.

In vbs I use dateadd for this and it looks like in c# there is the same http://msdn.microsoft.com/en-us/library/b1zcctd2(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

public static DateTime DateAdd(
      string Interval,
      double Number,
      Object DateValue
)

You just need to add or subtract the hours from UTC
0
 
LVL 18

Assisted Solution

by:Dennis Aries
Dennis Aries earned 250 total points
ID: 39902749
You can easily convert from UTC to another timezone:

// Note: userTimeZone is the string format of the timezone requested

TimeZoneInfo userZone = TimeZoneInfo.FindSystemTimeZoneById(userTimeZone);
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, userZone);

Open in new window


You can use
TimeZone.CurrentTimeZone

Open in new window

to retrieve the currently used timezone, but if you run the code on the server, then it will always return the server timezone (presumably UTC). For websites, it is common practise to have the user select a timezone in his/her profile and do the conversions according to that setting., in which case the first snippet will suffice.
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 39902824
There are two approches for this issue:
1) Keep all times server based time: In other words any UTC time will be converted to local server time before being rendered to the client browser.
Adv: Easy to implement.
Disadv: It does not take in account the client browser timezone.
This is the approach you are doing right now if you are coding in the server.

2) Transmit the time as UTC and let the browser display it as local time based on the client timezone machine in javascript:
var date = new Date('2012-11-29 17:00:34 UTC');
var timeAsString  = date.toString();

Open in new window

Adv:  It takes in account the client browser timezone.  
Disadv: More difficult to implement because it need to use Javascript to convert the times in the client browser. Keep in mind that it may be confusing for your support people if they work in different time zones, for example supprot team is in Sydney, UK and India, all of them will look at different reported time unless there are process in place to remind them that a time conversion is required.
0
 

Author Comment

by:websss
ID: 39902863
Actually this was the solution
public static String ConvertUtcDateTimeToLocalDateTime(DateTime utcTime, string localTimeZone)
        {
            //Pass in a UTC time and it will return the users Timezone date time
            DateTime localTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcTime, localTimeZone, "UTC");
            return localTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

Open in new window


will give points for effort
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question