Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

timezone headache - utc +10 as UTC

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
websss
Asked:
websss
2 Solutions
 
Scott Fell, EE MVEDeveloperCommented:
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
 
Dennis AriesCEO @ Arkro ITCommented:
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
 
Miguel OzSoftware EngineerCommented:
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
 
websssAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now