When the datetime type is passed into Asp.net Ajax, the time will change to the zero time zone.

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 the time in China.

For example:
Custom type myorder
For more information about how to use custom types in Asp.net Ajax, see
Use ASP. Net ajax to asynchronously call the class methods in Web Services and pages (6): automatic conversion of server and client data types: complex types
C #
Public class myorder ()
{
Public myorder ()
{
}

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 #
Public datetime tolocaltime ()
 

 

Return Value
The kind attribute of a datetime object is local, and its value is equivalent to the local time of the value of the current datetime object. If the converted value is too large, it cannot be represented by a datetime object, it is maxvalue, or if the converted value is too small to be expressed as a datetime object, it is minvalue.
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 Daylight Time rules of the time represented by the current datetime object.

Starting from. NET Framework 2.0, the value returned by the tolocaltime method is determined by the kind attribute of the current datetime object. The following table describes possible values.

Kind
Result
 
UTC
Datetime.
 
Local
No conversion is performed.
 
Unspecified
Assume that this instance of datetime is UTC time and kind is treated as UTC during conversion.
 

The value returned by this conversion is the kind property, which always returns the datetime of the local. Therefore, even if tolocaltime is repeatedly applied to the same datetime, 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.