ASP.NET System.DateTime not compatible with DateTime.Parse or SQLDateTime.Parse? RRS feed

  • Question

  • Hi all,

    I am currently having an issue with ASP.NET's datetime. It appears that the datetime format is not consistent and incompatible with some of it's own functions.

    Platform used:

    MacOS SierraEnvironment is Visual Studio for Mac, Community, Version 7.3 build 799.
    Active runtime is Mono Framework is .NET 4


    I have a .aspx.cs in which I have a class that inherits from System.Web.UI.Page.

    In my Page_LoadComplete function I declare a text field such as:

    System.DateTime str_date = DateTime.Now;
    Console.WriteLine("Webpage.aspx.cs str_date: " + str_date);
    [Console.Writeline output: 
    Webpage.aspx.cs str_date: 14/12/2017 9:57:07 AM]

    textfield.Text = str_date.ToString();
    textfield.Text: " + textfield.Text);  
    [Console.Writeline output: Webpage.aspx.cs str_date: 14/12/2017 9:57:07 AM]

    This field is then shown in my website.aspx through:

    <asp:TableCell><asp:Label runat="server" Text="" ID='timefield'></asp:Label></asp:TableCell> 

    The time is showing without issue on the website as: 14/12/2017 9:57:07 AM  

    In my Handler I am receiving this field through: timefield = context.Request["timefield"].ToString().Trim();
    [Console.Writeline output from handler textfield14/12/2017 9:57:07 AM]

    However in my Handler when I try to parse this string SqlDateTime.Parse(DateTime.Parse(timefield).ToString());
    I ALWAYS get:

    Application_Error: System.FormatException: String was not recognized as a valid DateTime.

    How is this even possible?? I created this through a basic system function! No user input, nothing. Visual output is ALWAYS the same.

    This value needs to be serialised to a database so I am basically trying to have it in the required format.

    At some point trying to serialise "timefield" without any parsing to either datetime or sqldatetime worked with any issue. The related code was not touched for weeks, but then suddenly it stopped working throwing an error message about the format. I then tried to "force" it to what should be the right format, without any luck.

    It looks like ASP.NET DateTime formats and functions are not consistent and even worse, not compatible with each other??!

    Any ideas how I could simply get my datetime variable and be able to safe this to the DB? I have been stuck on this for days now. 

    Many thanks in advance!

    • Moved by Sabah ShariqMVP Thursday, December 14, 2017 8:48 AM Moved From Visual C#
    Thursday, December 14, 2017 7:00 AM

All replies

  • Hi baoshenglishanghai,

    This is Visual C# forum. As your issue is related to Web please ask your question into ASP.Net forum for getting quick response. I am moving your thread to off topic.


    Your understanding and cooperation will be grateful.

    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Thursday, December 14, 2017 8:48 AM
  • Sorry, I thought I had selected ASP.NET prior to posting... Maybe mis-clicked.

    We found the issue:

    It was related to the server locale (currently my DEV computer).

    I changed my computer to d/M/yyyy h:mm:ss tt through the Mac settings. In the aspx.cs I changed to: textfield.Text = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"CultureInfo.InvariantCulture); SqlDateTime.Parse then works without any complaints. It appears it only worked because previously (until 12.12.) whatever date was in there could pass. Not sure how it was able to process
    days greater than 12... Clearly, server and datetime must be out of sync for it to work... 

    To be sure I will probably force this to a specific (local) datetime.

    Thursday, December 14, 2017 9:00 AM