1

I am using ASP.NET to provide some variables for the data that has to be added to a SQL database.

Code I am using:

I am using DateTime.Now to select the current time.

How I use it:

I have two pages, one is a page to insert the posts of the users. Other page is used for ajax purpose, to add some text comments to the posts.

In both page I use the same code.

But the code is executing different values. You can have a look here:

DateTime.Now

In the post the time is saved as "9/1/2013" which means 1st September, 2013! In comment it is saved as Sep 1 2013, which means the same.

My question: how does the code know that the request is an ajax one or the post one. The post page code is wrapped in if(IsPost) { however the comment is an ajax call.

What is the reason behind this?

4

3 回答 3

1

I found what the issue was.

I had set the column DataType to nvarchar(50) in the database table. After editing it to DataType DateTime I was able to get the same result. So the issue was not the Culture or DateTime. It was the DataType of the column in SQL Server Database.

于 2013-09-01T18:32:52.057 回答
0

Assuming that your AJAX call is not changing the format string of your DateTimes, I would assume the threads which process those 2 different requests are operating under different cultures, which would explain why they're being displayed differently (not sure why though, check your settings perhaps). Try the following and you'll see how culture can effect DateTime output:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
Console.WriteLine (DateTime.Now.ToString());
Thread.CurrentThread.CurrentCulture = new CultureInfo("gu-IN");
Console.WriteLine (DateTime.Now.ToString());

Here's some additional information on CultureInfo.

于 2013-09-01T10:28:48.633 回答
0

Code knows from Current thread's Culture information. You should use DateTime.Now.ToString("yyyy-MMM-dd") or any other formate according to your needs to be sure of output.

于 2013-09-01T10:29:49.453 回答