0

I've been using DateTime.Now in my asp.net application. However, I think this is always using the time of the server which is in the US. How could I get the datetime based on the timezone? For example, if someone is using my software in Taiwan I would want to show the date based on their timezone not on US timezone.

I know that I can find the current timezone like this:

TimeZone localZone = TimeZone.CurrentTimeZone;

However, I need a DateTiem. Is there a way I could convert this to a DateTime?

Edit

To clarify, I want to just show the current date on my webpage. However, when I use DateTime.Now it uses the timezone of my server. I won't be storing this date in my DB so I don't think I'll need this date in UTC.

roshambo
  • 97
  • 2
  • 11
  • 2
    You need to detect the brower's time zone. There are *lots* of questions about that. – Jon Skeet Jul 28 '14 at 06:11
  • To specifically answer your question: http://stackoverflow.com/questions/9869051/how-to-convert-datetime-in-specific-timezone – Kajzer Jul 28 '14 at 06:12
  • Do you actually need the user's time zone, or just the current UTC offset in their time zone? The latter is significantly easier to achieve. – Jon Skeet Jul 28 '14 at 06:13
  • @JonSkeet I guess the second. Then I could just convert UTC to current DateTime right? – roshambo Jul 28 '14 at 06:15
  • @roshambo: Well it's not clear what you're actually trying to achieve. We have no idea what your application is doing. If you only need to record a timestamp, just use `DateTime.UtcNow`... that doesn't give you time zone information, but it records the right point in time, which can be converted to any other time zone later. It's very hard to give good advice with so little context - choosing the right solution to date/time issues is very context-dependent. – Jon Skeet Jul 28 '14 at 06:17
  • @JonSkeet I've edited my question. I want a way to just display the current date on the page. I wont need a timestamp. I would want to just set an asp.net label using my c# code that would show the date. thanks – roshambo Jul 28 '14 at 06:27
  • 1
    Why do you need that to be done server-side at all? Do all the formatting client-side (e.g. with moment.js if necessary) and it'll use the browser's time zone. That's much simpler than telling the server what the time zone is. – Jon Skeet Jul 28 '14 at 07:56

1 Answers1

-1

This will convert local time, to specific time zone:

var yourTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById("your time zone ID"));
Uriil
  • 11,948
  • 11
  • 47
  • 68