0

I want to get client timezone so i'm using the code below

function filltime() 
{
        document.getElementById("hdnTime").value = new Date();
} 

conversion

 Dim time As Date = DateTime.ParseExact(hdnTime.Value,
                                           "ddd MMM d HH:mm:ss UTCzzzzz yyyy",InvariantCulture)  

I'm not getting exact value. Its showing server time only. But hdnTime.Value contains the correct value("Mon Feb 18 14:46:49 UTC+0530 2013"). I think the problem is with conversion.

What is the problem? How can I solve?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
balaji
  • 1,236
  • 2
  • 18
  • 28

2 Answers2

2

You are confusing the DateTime object with its displaying

it is normal that you see the server time because you are seeing the datetime rappresentation with your current timezone.

What you don't get is how DateTime works...

If you pass a datetime with timezone info then it will be converted to your timezone with the right offset.

If you want to pass a datetime and get it as it is than you have to remove the timezone part.

In you situation, anyway, if you need only to know the client timezone just pass it!

var d = new Date()
var n = d.getTimezoneOffset();

The getTimezoneOffset() method returns the time difference between UTC time and local time, in minutes.

For example, If your time zone is GMT+2, -120 will be returned.

For general discussion: in my experience the best way to deal with datetime converted as string and passed between different systems, is to use the ISODATE format:

DateTime.Now.ToString("s"); //"2013-02-18T11:17:24"
giammin
  • 18,620
  • 8
  • 71
  • 89
  • I'm not getting you. "Mon Feb 18 14:46:49 UTC+0530 2013" is my javascript return string. just i want to assign this to datatype "date" – balaji Feb 18 '13 at 10:37
  • Thanks. Now only I'm understanding the real problem – balaji Feb 19 '13 at 06:40
2

Dates and times are a pain in 1 language, let alone when passing a value between 2.

I'd recommend serializing the JavaScript Date() object into JSON before posting it back to the server. Then de-serializing it into a C# DateTime object using a library such as JSON.NET. There's comprehensive documentation (Serializing Dates in JSON) with regards to what settings can be applied when serializing and de-serializing.

JavaScript

function filltime() 
{
    document.getElementById("hdnTime").value = JSON.stringify(new Date());
}

JSON isn't native to every browser, so you mean need to manually load it in, for more information you can refer to: Browser-native JSON support (window.JSON)

C# using JSON.NET

DateTime dateTime = JsonConvert.DeserializeObject<DateTime>(hdnTime.Value);
Community
  • 1
  • 1
Richard
  • 8,110
  • 3
  • 36
  • 59
  • 1
    @balaji in this way you lose timezone info as i say in my answer but you will have a non readable value instead of using ISOdate – giammin Feb 18 '13 at 13:31