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. .
This displays the clock like this .
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?
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).
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.
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.
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).
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?
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With