Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Sharp DateTime Format

UPDATE This looks to be a bug in Windows 7. I tested the same scenario with Windows 8 and I cannot replicate this there. Please see the MS Bug Report that I posted on this issue if you want more information. Thank you again to all that helped.

UPDATE 2 The error happens on Server 2008 R2 as well (Kind of expected that)

Original Submission

Using the examples on the following page Date Formats I am able to control the format of my date. However, one of my clients, using Windows 7, modified their calendar to display their short date like this 'ddd MM/dd/yy', see the image for the settings. Short Date Format.

This displays the clock like this enter image description here.

This works fine except when I use a date on their machine. When I format the date like the following...

String.Format("{0:MM/dd/yy}", dt); //the result is 06 04 13, notice the spaces

If I take off the ddd to display the day of week in the calendar settings and use the same format option I see the following...

String.Format("{0:MM/dd/yy}", dt); //the result is 06/04/13, this time it has forward slashes

The .ToShortDateString() option on the date gives me "Tue 06/04/13" and crashes when going into a database. This is how the issue was found.

Outside of hard coding the format, i.e. joining the month to the forward slash to the day etc, does anyone know of what else I can try to get this to work?

like image 505
JabberwockyDecompiler Avatar asked Jun 04 '13 17:06

JabberwockyDecompiler


People also ask

What is T and Z in time format?

What is T between date and time? The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

Is there a date type in C#?

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.

What is FFF in date format?

The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.

What is DateTimeOffset C #?

Remarks. The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).


2 Answers

It sounds like you are formatting the date as a string in order to send it in via some SQL. Have you considered using command parameters for this instead of string formatting?

like image 105
Michael Gunter Avatar answered Sep 20 '22 08:09

Michael Gunter


Using the InvariantCulture should work. I created a test console app to check it. The code changes the thread's current culture to be the Invariant one:

class Program
{
    static void Main(string[] args)
    {
            /// Displays '06 04 13'

        Console.WriteLine(string.Format("{0:MM/dd/yy}", System.DateTime.Now));

        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            /// Displays '06/04/13'

        Console.WriteLine(string.Format("{0:MM/dd/yy}", System.DateTime.Now));

        Console.ReadLine();
    }
}
like image 30
crash Avatar answered Sep 22 '22 08:09

crash