browse by category or date

.NET Framework

Contents

By .NET Framework I mean C# :D. In C#, converting a DateTime object to a String is by calling its ToString method. On the other hand, converting a String to a DateTime is by using either DateTime.Parse or DateTime.ParseExact.

Console.WriteLine(DateTime.Now.ToString());
// Output: 3/21/2015 5:50:43 AM

Console.WriteLine(DateTime.Now.ToString("dd-MMM-yyyy"));
// Output: 21-Mar-2015

var dt = DateTime.Parse("19 Mar 2012"); 
// Works

var dt1 = DateTime.Parse("19 03 2012"); 
// Exception, because my default the US Way

var dt2 = DateTime.Parse("03 19 2012"); 
// Only works if your default culture has date format similar to German, e.g. Italy

// Now we parse with custom format
var dt3 = DateTime.ParseExact("19 03 2012", "dd MM yyyy", System.Globalization.CultureInfo.InvariantCulture)
// Works

As we can see, we may pass the custom format to DateTime.ParseExact and DateTime.ToString. The symbols which we can use in the custom format is exactly the same as the symbols we use in MS SQL (please refer to the previous page).

Hopefully, converting between DateTime to String, vice-versa, would no longer a problem. You can always return here for a quick reference.

I hope it helps,
Cheers

GD Star Rating
loading...
Working With Custom DateTime Format In MySQL, Oracle, MS SQL and .NET, 5.0 out of 5 based on 1 rating

Possibly relevant:

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Incoming Search

.net, c#, mssql, mysql, oracle

No Comment

Add Your Comment