locked
Today.Date format causes errors RRS feed

  • Question

  • User186656733 posted

    I am getting today's date using Today.Date and formatting it as needed.

    In one case, Today.Date looks like this: 11.8.2014

    I try to format it like this: myDate = Format(Today.Date, "M/d/yyyy")

    but the resulting string still looks like the original (11.8.2014).

    Seems simple enough - what am I missing?

    Help most appreciated.

    Saturday, November 8, 2014 10:21 AM

Answers

  • User281315223 posted

    When it comes to actually formatting DateTime objects within ASP.NET, you need to ensure they are actually the appropriate types (e.g. DateTime objects). For example if you have a stringified DateTime that is in a format like this "11.8.2014", you'll need to properly parse that as a DateTime :

    // C# Example
    DateTime myDate = DateTime.ParseExact(yourDateString,"M.d.yyyy");
    ' VB Example
    Dim myDate = DateTime.Parse(yourDateString,"M.d.yyyy")

    After you have your date in a DateTime variable, you can then simply use the ToString() method to pass in the formatting string that you need to use :

    // C# Example
    var formatted = myDate.ToString("M/d/yyyy");
    ' VB Example
    Dim formatted = myDate.ToString("M/d/yyyy")

    If you already have a DateTime variable such as DateTime.Now. or DateTime.Now.Today, you can just use those directly :

    // C# Example
    var formatted = DateTime.Now.Today.ToString("M/d/yyyy");
    ' VB Example
    Dim formatted = DateTime.Now.Today.ToString("M/d/yyyy")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2014 11:28 AM
  • User753101303 posted

    Then it is likely not needed. The problem is likeky that your turn this date into a string as part of a SQL statement and the db will turn this string into a date to store whatever value is the internal representation for this date.

    Instead you can use a "parameterized query" (just search you should find tons of samples) that is you'll have a SQL statement such as:
    INSERT INTO MyTable(DateCol) VALUES (@DateParam)

    Then  your code provides the value for this parameter Under its native format (that is Today.Date for example) and it will pass to the server in a way that always works regardless of client side or server side settings.

    Here even if it works with this particular server, it wiill break if using against another server. For example for SQL Server the only format that always work is:
    http://technet.microsoft.com/en-us/library/ms180878(v=SQL.105).aspx#StringLiteralDateandTimeFormats

    So rather than having to explicitely convert values to string using the format which is needed (and may have the problem for numbers or even for strings for example if they include a ' character you need to double the '), use parameterized queries and pass the actual value (ie a date, a string, a boolean whatever) and it will just work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2014 12:36 PM
  • User-158764254 posted

    I try to format it like this: myDate = Format(Today.Date, "M/d/yyyy")

    but the resulting string still looks like the original (11.8.2014).

    The / character is a special replacement character when you are formatting a datetime.  http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#dateSeparator 

    So just as you do not get a literal M when you put M in the format string (its replaced with the month integer), you also are not guaranteed to get a / when you put a / in the format string (its replaced with the dateseperator for the culture your code is running under).

    I expected Format() to work regardless of the actual format of Today.Date.

    Is there a better way to convert Today.Date to format M/d/yyyy?

    If your goal is that the forward slashes are literal characters and should not be replaced by the dateseperator for your current culture, then  you can specify them as literals like this:

    string myDate =  DateTime.Today.ToString("M'/'d'/'yyyy");
    Console.WriteLine(myDate);

    or you can escape the forward slash like this (http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#escape):

    string myDate =  DateTime.Today.ToString(@"M\/d\/yyyy");
    Console.WriteLine(myDate);

    or like this:

    string myDate =  DateTime.Today.ToString("M\\/d\\/yyyy");
    Console.WriteLine(myDate);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 9, 2014 9:38 AM
  • User281315223 posted

    I guess at the heart of the matter is this question:  where do DateTime.Now or DateTime.Today get their formats?

    The server? the client?  Somewhere in IIS?

    By default, DateTime objects do not use any inherent formatting rules (e.g. a DateTime.Now or DateTime.Today object is just going to be a DateTime). Formatting only comes into play when the DateTime object is being output as a string.

    If a particular formatting string is not defined, the default culture will be used. You can change this if you need to handle a specific other culture within the web.config file in your application as follows :

    <configuration>
        <system.web>
            <!-- Set your preferred culture here -->
            <globalization uiCulture="en-US" culture="en-US" />
        </system.web>
    </configuration>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 10, 2014 9:12 AM

All replies

  • User753101303 posted

    Hi,

    What is your country settings?

    As long as you are using a native type there is no problem. If I assign Today.Date to a variable I'll get the same date value than you. The problem is that if I start to LOOK at this value, it will be shown according to MY country convention (08/11/2014 here in France).

    You have the same problem for numeric values (depending on the country, . or , is the separator and the thousand separator is not the same etc...).

    So:
    1) The value of Today.Date you see is not a problem. As long as you are using the native datetime type you have the same value than anyone else on earth at the same day
    When you convert to or from a string you must think about which country convention is in used as how the value is CONVERTED to/from a string depend on your country convention

    For example if you'll save this value to a db this is not a problem.

    2) Currently, in the format you used / is the date separator for your country. So it seems to be a "." and it works as expected. It dépends your intent:
    - if you want to write some code that can work regardless of the country convention, it might be not a problem
    - if you want to force a particular country convention, you could change your web.config file to force a particular country convention
    - for some need you have also a special "country" convention (InvariantCulture) which is guaranteed to never change (and can't even be customized)

    Saturday, November 8, 2014 10:37 AM
  • User-1716253493 posted
    Is myDate string? Format output is a string. myDate should be string type.
    Saturday, November 8, 2014 10:38 AM
  • User186656733 posted

    oned_gk,

    Thank you for your quick reply.

    Yes, myDate is a string.

    Saturday, November 8, 2014 10:44 AM
  • User186656733 posted

    PatriceSc,

    Thank you for your quick reply.

    I want to convert Today.Date to a specific format because the server is located in US, and the database expects this format - M/d/yyyy.  If I query the database with any other format - error.

    I expected Format() to work regardless of the actual format of Today.Date.

    Is there a better way to convert Today.Date to format M/d/yyyy?

    Thank you.

    Saturday, November 8, 2014 10:50 AM
  • User-1716253493 posted
    Try : myDate = DateTime.Now. ToString("M/d/yyyy")
    Saturday, November 8, 2014 11:09 AM
  • User2103319870 posted

    You can also use String.Format to convert the dataetime to your required format.

    You can try with the below code

    //Get Todays date here
    		DateTime today = DateTime.Today;
    		
    		//Format the date as per your required date format 
    		string mydate = String.Format("{0:M/d/yyyy}", today); //outputs 11/8/2014
    Saturday, November 8, 2014 11:19 AM
  • User281315223 posted

    When it comes to actually formatting DateTime objects within ASP.NET, you need to ensure they are actually the appropriate types (e.g. DateTime objects). For example if you have a stringified DateTime that is in a format like this "11.8.2014", you'll need to properly parse that as a DateTime :

    // C# Example
    DateTime myDate = DateTime.ParseExact(yourDateString,"M.d.yyyy");
    ' VB Example
    Dim myDate = DateTime.Parse(yourDateString,"M.d.yyyy")

    After you have your date in a DateTime variable, you can then simply use the ToString() method to pass in the formatting string that you need to use :

    // C# Example
    var formatted = myDate.ToString("M/d/yyyy");
    ' VB Example
    Dim formatted = myDate.ToString("M/d/yyyy")

    If you already have a DateTime variable such as DateTime.Now. or DateTime.Now.Today, you can just use those directly :

    // C# Example
    var formatted = DateTime.Now.Today.ToString("M/d/yyyy");
    ' VB Example
    Dim formatted = DateTime.Now.Today.ToString("M/d/yyyy")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2014 11:28 AM
  • User753101303 posted

    Then it is likely not needed. The problem is likeky that your turn this date into a string as part of a SQL statement and the db will turn this string into a date to store whatever value is the internal representation for this date.

    Instead you can use a "parameterized query" (just search you should find tons of samples) that is you'll have a SQL statement such as:
    INSERT INTO MyTable(DateCol) VALUES (@DateParam)

    Then  your code provides the value for this parameter Under its native format (that is Today.Date for example) and it will pass to the server in a way that always works regardless of client side or server side settings.

    Here even if it works with this particular server, it wiill break if using against another server. For example for SQL Server the only format that always work is:
    http://technet.microsoft.com/en-us/library/ms180878(v=SQL.105).aspx#StringLiteralDateandTimeFormats

    So rather than having to explicitely convert values to string using the format which is needed (and may have the problem for numbers or even for strings for example if they include a ' character you need to double the '), use parameterized queries and pass the actual value (ie a date, a string, a boolean whatever) and it will just work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 8, 2014 12:36 PM
  • User-158764254 posted

    I try to format it like this: myDate = Format(Today.Date, "M/d/yyyy")

    but the resulting string still looks like the original (11.8.2014).

    The / character is a special replacement character when you are formatting a datetime.  http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#dateSeparator 

    So just as you do not get a literal M when you put M in the format string (its replaced with the month integer), you also are not guaranteed to get a / when you put a / in the format string (its replaced with the dateseperator for the culture your code is running under).

    I expected Format() to work regardless of the actual format of Today.Date.

    Is there a better way to convert Today.Date to format M/d/yyyy?

    If your goal is that the forward slashes are literal characters and should not be replaced by the dateseperator for your current culture, then  you can specify them as literals like this:

    string myDate =  DateTime.Today.ToString("M'/'d'/'yyyy");
    Console.WriteLine(myDate);

    or you can escape the forward slash like this (http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#escape):

    string myDate =  DateTime.Today.ToString(@"M\/d\/yyyy");
    Console.WriteLine(myDate);

    or like this:

    string myDate =  DateTime.Today.ToString("M\\/d\\/yyyy");
    Console.WriteLine(myDate);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 9, 2014 9:38 AM
  • User-1716253493 posted
    Simple query : WHERE DATEFIELD = CAST(GETDATE() AS DATE)
    Sunday, November 9, 2014 10:30 AM
  • User186656733 posted

    Thank you for all these suggestions.

    I guess at the heart of the matter is this question:  where do DateTime.Now or DateTime.Today get their formats?

    The server? the client?  Somewhere in IIS?

    Sunday, November 9, 2014 11:28 PM
  • User-1716253493 posted

    Saavik

    Thank you for all these suggestions.

    I guess at the heart of the matter is this question:  where do DateTime.Now or DateTime.Today get their formats?

    The server? the client?  Somewhere in IIS?

    DateTime value actualy don't have specific format, it's only a value without formating

    If you pass datetime value as parameter, you don't need to tink about date format.

    If you format date to a string in ASP.Net (ie. Label.Text) or showing date in GridView it's depent culture setting in your application.

    AFAIK by default, It's IIS server language. you can also set web.config to specific culture/uiculture.

    You can also set culture/uiculture in page directive for single page only.

    If you convert the date to varchar in sql query, it's depend sql server date format.

    If you still work with datetime type, dont worry about the format.

    If you pass a string to pass date value to sql server, you need to think about sql format.

    Monday, November 10, 2014 1:50 AM
  • User281315223 posted

    I guess at the heart of the matter is this question:  where do DateTime.Now or DateTime.Today get their formats?

    The server? the client?  Somewhere in IIS?

    By default, DateTime objects do not use any inherent formatting rules (e.g. a DateTime.Now or DateTime.Today object is just going to be a DateTime). Formatting only comes into play when the DateTime object is being output as a string.

    If a particular formatting string is not defined, the default culture will be used. You can change this if you need to handle a specific other culture within the web.config file in your application as follows :

    <configuration>
        <system.web>
            <!-- Set your preferred culture here -->
            <globalization uiCulture="en-US" culture="en-US" />
        </system.web>
    </configuration>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 10, 2014 9:12 AM