(Original) enforces datetime Constraints

Source: Internet
Author: User

Enforce datetime Constraints

InProgramWhen we need to display the date, we often get used to using datetime. Now. tostring (), but we do not know that there are some hidden dangers in doing so.

The time format is associated with the Cultural Region of the current system. If the region of your operating system is set to China, for the tostring () method of datetime, it is equivalent Datetime . Now. tostring ( "G" , New Cultureinfo ( " ZH - CN " )) , Shown as 18:20:31, and if your region is set to the United States, then the program will display 4/16/2009 6:03:11, in this wayCodeUsed in Datetime . Now. tostring () If, once the program is migrated to a machine in another country, it is displayed in the program as inconsistent with the time string in the application, which may cause some difficult troubleshooting exceptions.

I have encountered such a problem. I wrote a log Writing Method locally and used the current date as the file name. There is no problem locally, and a file such as 2009-4-16.log will be created correctly, however, when a program is deployed to a server in the United States, this method will report an error. Because, based on the region settings of the U.S. server, the program will create something similar to 4/16/2009. log files, but obviously, because of the existence of/, Windows cannot have such a file name, so it will fail. There is also the datetime stored in the database, which is usually produced in the United States and written to the database at 6:03:11 AM on. However, in the support program in China, however, it may cause some troubles and errors.

Therefore, we need to ensure the consistency of time display in the program. Try not to use the datetime. Now. tostring () format, but use the format similar Datetime . Now. tostring ( "G" , New Cultureinfo ( " ZH - CN " )) To reduce the error probability in the project. If this writing method is too difficult, you can add an anonymous method for datetime to facilitate the call. The Code is as follows:

Public   Static   Class Datetimeextend
{
Public   Static   String Tostdstring ( This Datetime DT)
{
Return Datetime. Now. tostring ( " G " , New Cultureinfo ( " Zh-CN " ));
}
}

 

Well, this can be used anywhere in the program.Tostdstring() Method to convert a datetime to a standard string, which has the same value in any environment.

The above is all, hope to help you! O_o

 

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.