The problem of time changes after the datetime type is passed into Asp.net Ajax

Source: Internet
Author: User
Tags mscorlib

after the datetime type is passed into Asp.net Ajax, the time will change to the zero time zone. In other words, the time is 8 hours less than that in China.
example:
Custom type myorder
for more information about using custom types in Asp.net Ajax, see
using ASP. net Ajax asynchronous call of class methods in Web Services and pages (6): automatic conversion of server and client data types: complex types
C #
public class myorder ()
{< br> Public myorder ()
{< BR >}< br>
private datetime _ orderdatetime;

Public datetime orderdatetime
{
Get
{
Return _ orderdatetime;
}
Set
{
_ Orderdatetime = value;
}
}
............ Others
}

JS

Function ()
{
VaR order = new myorder ();
Myorder. orderdatetime = new date ();
............ Others
}

Then, check in WebService. After the input, orderdatetime is less than 8 hours.

If you directly input SQL into the database using the datetime parameter, the normal time is returned.

At last, we found a problem with the time zone.

The solution is to call the tolocaltime () method of datetime. If the time zone of the Local Computer is set correctly, the returned time zone is also correct.
Some materials have been queried, and 8 hours can be added directly. However, the input time includes the time zone information. It seems better to use tolocaltime. In addition, I tried to set web. config to <globalization culture = "ZH-CN" enableclientbasedculture = "false"/> but it does not work.

About tolocaltime

Converts the value of the current datetime object to the local time.

Namespace:System
ProgramSet:Mscorlib (in mscorlib. dll)

Syntax

C #
PublicDatetimeTolocaltime()

Return Value

OneDatetimeObject whose kind attribute is And its value is equivalent to the currentDatetimeThe local time of the object value. If the converted value is too largeDatetimeIf the converted value is too small, it cannot be expressedDatetimeMinvalue.Remarks

The local time is equal to the UTC time plus the UTC offset. For more information about the UTC offset, see timezone. getutcoffset. The conversion should also be applied to the currentDatetimeThe daylight saving time rule of the time indicated by the object.

Starting from. NET Framework 2.0,TolocaltimeThe value returned by the method isDatetimeObjectKindConfirm the property. The following table describes possible values.

Kind

Result

DatetimeIs converted to local time.

Local

No conversion is performed.

Assume thatDatetimeThe instance is UTC time, andKindAsUTC.

The value returned by this conversion isKindAttribute always returnsLocalOfDatetime. Therefore, even ifTolocaltimeApply to the sameDatetime, The returned result is still valid.

This method always uses the local time zone for calculation.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.