The problem with the SQLite Database "the string is not recognized as a valid datetime

Source: Internet
Author: User
Tags iso 8601

I haven't found a reasonable explanation for a long time. Fortunately, I found an articleArticle, To answer this question, this is a special favorite for your reference.

Http://www.xueit.com/html/2009-02/21_644_00.html

Recently I used ASP. NET + SQLite database to develop a website.ProgramThe result is a headache in date processing.

 

Note:An error occurred while executing the current Web request. Check the stack trace information to learn about this error andCodeDetailed information about the cause of the error.
Exception details:System. formatexception: the string is not recognized as a valid datetime.

 

Solution:

Converts a type when the date is saved to the SQLite database, for example, string _ now = system. datetime. Now. tostring ("S ");

That is to say, adding s to the. tostring () method solves the problem of date reading errors.

Simple code example:

String _ indate = request ["indate"]; // The input date, for example, 2009-2-21
Datetime _ intime = convert. todatetime (_ indate );

// The SQL statement for saving data is as follows:

Insert into table (indate) values ('"+ _ intime. tostring (" S ") +"'); // convert

 

The following date is saved in the SQLite database:

 

The strange reason is that the date is 1899-12-30. Rest assured that this is normal. Let's see what I read from the date below? As follows:

 

See, it is the correct date.

 

Because ISO 8601 Description: (http://msdn.microsoft.com/zh-cn/library/ms187819.aspx)

A string example is provided:
* 2004-05-23t14: 25: 10
* 2004-05-23t14: 25: 10.487

Therefore, the date saved by SQLite must be converted to ISO 8601 standard string format.

Use date. tostring ("S"); this method is converted to ISO 8601 standard string format.

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.