473,883 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET AJAX Date serialization and timezones

I have a ASP.NET Ajax app (using client library) calling ASP.NET Ajax-enabled
web services. We are making use of the javascript proxies generated by
ASP.NET Ajax.

The problem we have is that the UTC dates sent by the browser are being
adjusted for timezones by the server, but we do not want dates adjusted for
timezones.

For example, the user enters the date of a transaction as '25 May 2007'.
That ends up as a javascript Date() instance represented as 'Fri May 25
00:00:00 UTC+0100 2007'. This seems fine. But when it ends up in the web
service code, the 1 hour has been subtracted and the date has become
{24/05/2007 23:00:00}. We are not insterested in the time, only the date, and
the date is now wrong.

Is there a way in javascript to get rid of the timezone information from the
date instance? Or is there a way to configure the JSON
serialisation/deserialisation to ignore the timezone? I have hundreds of
objects in the system that use dates, so ideally I want to be able to solve
this "in one place" - custom processing of each date property sends shivers
down my spine.

I'd be grateful for any help!

Stu
Mar 22 '07 #1
7 4269
Hi Stu,

I've carefully reviewed your question and I'm currently consulting your
question within our internal discussion list. In the meanwhile, would you
please elaborate more on how can I reproduce the issue on my side? I'm
interested in the detailed setup or steps on your side. Thanks.
Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 23 '07 #2
Hi Walter,

My Date and Time settings:
Current timezone: GMT Standard Time
Auto adjust for daylight savings: true

My regional settings are:
Standards and fomats: English (United Kingdom)
Location: United Kingdom

To repro this:
- Set your regional and date time settings as above.
- Setup an ASP.NET AJAX web service with a web method that accepts a
DateTime parameter
- Put a breakpoint inside that method.
- Reference that web service from an ASP.NET AJAX web form.
- In client JavaScript for the web form create a variable to hold a date:
var myDate = new Date(2007, 5 - 1, 29); // months are zero-based
- add a "debugger;" statement directly after this variable assignment.
- After the debugger statement, setup a call to the web service passing
myDate as the parameter.
- Run the page in Debug mode and invoke your javascript.
- When it breaks in the javascript, inspect myDate and observe that its
string representation is "Tue May 29 00:00:00 UTC+0100 2007".
- Continue execution
- When it breaks in the web service, observe that the the value of the date
parameter has been reduced by one hour: 28/05/2007 23:00:00
- in the immediate window invoke .ToLocalTime() on the date parameter.
Observe that the date is restored to the orginal 29/05/2007 00:00:00.

There are actually two issues here:
1. This is all on my dev machine - so why on earth does my browser report
UTC+1 and my web service just UTC?
2. Regardless of my machine discrepancy, in production we will have users
all over the world. When they enter a date into a form I can't have the dates
being adjusted for timezone differences.

I notice that there are attributes to constrain XML Serialization of a
DateTime to only use the Date part. Is there anything similar for JSON
serialization?
I do not want to perform .ToLocalTime() in all web services in the system,
as we are bound to miss something - and these dates are crucial.

Is there any elegant way to make sure that a DateTime instance on the server
will always have the same actual date and time values as the original
JavaScript Date() instance?

Let me know if you need any more information.

Many thanks

Stu
Mar 23 '07 #3
Hi Stu,

Thank you very much for your detailed repro steps. I can now clearly see
the issue you described.

I hope I've understood your objective correctly: your objective is to get
client-side date part on server-side so that you can use it to generate
some string representation and return it back to client.

I think this is unfortunately not possible if you're transferring a
DateTime type parameter, this is because only a UTC datetime gets
transferred and the client-side timezone information is not transferred to
server.

Nikhil Kothari has once discussed how the Date gets serialized in JSON:

#Nikhil Kothari's Weblog : Wishful Thinking - Date Literal Syntax for JSON
http://www.nikhilk.net/DateSyntaxForJSON.aspx

#Nikhil Kothari's Weblog : JSON Serialization of Dates (Take 2)
http://www.nikhilk.net/DateSyntaxForJSON2.aspx

So far I'm not aware of any way to customize how ASP.NET AJAX library
serializes/deserializes JSON objects. I'm afraid you will have to pass the
year/month/day parts separately to the server if you want to generate a
string representation based on these information.
Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 26 '07 #4
Hi Stu,

Have you seen my last reply?

I understand you may have some concerns with this issue, you're welcome to
submit your feedback at
http://connect.microsoft.com/Main/co...ContentID=2220 which
is monitored by our product team. Thank you for your understanding.
Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 28 '07 #5
Hi Walter,

Thanks for looking into this and sorry for my late reply (I have been on
holiday).

I understand why the code works like it does for date+time combinations.
However, my requirement is to work with a date only (regardless of the the
time), and I believe this is such a common scenario that it is really
unacceptable to use the work around of using strings instead of DateTime
fields.

If I have an Employee class with a Birthday property, I would want to use a
Date type (I'm not interested in the time). However, the big problem is that
the .NET framework does not have a native Date type. So, I use DateTime type.
I don't want to use strings because I want to take advantage of strong typing
and I don't want to lose the date-related services provided by the DateTime
class.

If it was just a one off case of Employee.Birthd ay then a workaround
wouldn't seem so bad. But I have hundreds of Dates in my application! For
that reason I also don't want to implement an additional Timezone parameter
for each date property.

There are only two solutions that I think would be acceptable:

a) provide a Date type in the framework (I realise this would be a huge
change - but really useful)

b) somewhere in the ASP.NET Ajax framework provide a way of flagging a
DateTime property on a server class so that when the object is sent back to
the server the framework doesn't change the date part due to timezone
transformation. Surely this can't be too difficult.

An attribute would be perfect, e.g.

[IgnoreTime]
public DateTime IssueDate
{
get { ... }
set { ... }
}

The Ajax framework is great, I just really hope someone can find a way to
support this scenario without reverting to strings or timezone parameters.

Many thanks for your help so far,

Stu

Apr 16 '07 #6
Hi Stu,

I fully understand your concern on this question. Your feedback on the
DateTime data type is great. Please feel free to submit them on our connect
site: http://connect.microsoft.com/Main/co...ContentID=2220

For now, the best workaround would have to be storing the client-side
timezone information of each user on the server separately. For example,
many forum software will have a timezone information in every user's
profile and this will help the server to better format the date in the
forum.

You could either store this timezone information in Session or using
ASP.NET 2.0 Profile and update it whenever a user login. On the
server-side, when a DateTime variable is passed from client to server,
server-side code can first determine if the DateTime is a UTC time and
convert it back to a local time using the user's client-side timezone
information.

You can use following class to help you convert the UTC time to a local
time using a specified timezone since DateTime.ToLoca lTime() always uses
server-side timezone information.

#.Net Zone: Convert local time to Timezone.
http://staceyw.spaces. live.com/blog/cns!F4A38E96E59 8161E!931.entry

I understand this answer still not a perfect one for your scenario since
you're already having many existing code using DateTime information.
However, based on my research so far, the client-side JSON serialization
seems always using the UTC format to pass the client-side Date object to
the server. Creating a customized JavaScriptConve rter for the DateTime
class can only affect the way of how server passes back a DateTime and not
vice versa.

Regards,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 18 '07 #7
Walter, thanks very much for your help on this.

I will certainly provide feedback to the connect site.

Thanks again

Stu
Apr 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6136
by: | last post by:
Hello, I need to send a Java date (milliseconds since the epoch 1970-01-01 00:00:00.000 GMT) through a socket to a C++ app and reconstruct exactly the same date in the C++ app as in the Java app. The date (actually the milliseconds since the epoch) is splitted in 8 bytes and sent through the socket. How can I recalculate the date in c++? Are there methods / classes for this
3
35126
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
7
16247
by: developer | last post by:
I want to substract a number of month from a specific date. someone have a easy solution ? Thanks
14
3707
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style of date formatting (RFC 822). I have been using a variation of RFC 1123 (just change the time zone to an offset, i.e. "-0800"). It seems to be writing okay, but it's failing to parse. I've tried changing the regional & language settings in my...
2
2367
by: LandWolf | last post by:
Hi, I have a Web services write in Java and I want to write a client in C#. My web services work with Weather prevision. Hence, I set a date and my web services return the prevision for that date. When the web service return the date, this date is wrong. Es: Choose date: 24-apr-2006 Responce of Web service: day: 23-apr-2006, condition: sunny, temperature: 28
27
5337
by: Sanjay | last post by:
Hi All, I am using pytz.common_timezones to populate the timezone combo box of some user registration form. But as it has so many timezones (around 400), it is a bit confusing to the users. Is there a smaller and more practical set? If not, some suggestions on how to handle the registration form effectively would help me a lot. thanks Sanjay
2
3691
by: =?Utf-8?B?Sm9ubnk=?= | last post by:
I have an ASP.NET 2.0 C# web application that is contacting an Exchange server using WEBDAV. It allows the users to look up appointments for a future date. The problem I have is determining the correct client time zone. Note that Exchange stores everything in UTC time. I am able currently able to use javascript to ask the client what time zone they are in, but this is only for the "today" (the current day) and not for a day in the...
10
4041
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one problem. This function does not recognize time zones. How to adjust date to user's time zone? Is converting to timestamp() and then to format readable to visitors the one and only solution (e.g. strtotime() + date() OR DateTime object)? Perhaps,...
11
3064
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have run into a situation that if a page/tab that uses the Ajax toolkit (using .net version 3.5) is closed before the Ajax enable controls complete loading, then IE locks up. Does it in both IE7 and IE8. There is no issue when the controls are allowed to complete loading. Can you please tell me the best practice that handles this? Thanks.
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.