Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 36,512 times

Related Categories

Parse a UK Date String

By default, the DateTime.Parse() method assumes that dates given to it are in US format. In order to get it to process a non-US format date, such as the UK dd/mm/yyyy format, we need to pass it the appropriate "culture" information, like so:

C#

// we need to have imported System.Globalization
// using System.Globalization;

// fetch the en-GB culture
CultureInfo ukCulture = new CultureInfo("en-GB");
// pass the DateTimeFormat information to DateTime.Parse
DateTime myDateTime = DateTime.Parse("18/09/2004",ukCulture.DateTimeFormat);

VB.NET

' we need to have imported System.Globalization
' Imports System.Globalization

' fetch the en-GB culture
Dim ukCulture As CultureInfo = New CultureInfo("en-GB")
' pass the DateTimeFormat information to DateTime.Parse
Dim myDateTime As DateTime = DateTime.Parse("18/09/2004", ukCulture.DateTimeFormat)
 

Once converted, you can then call things like myDateTime.ToShortDateString() and actually get the format you expect - nice! :)

In case you're wondering what that en-GB bit is actually about - the first two characters give the ISO Language Code (ISO 639), and the second two give the ISO Country Code (ISO 3166).

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [4690] Parse a UK Date String

    Posted by davidxavier77 on 13 Aug 2006

    if you want it in a particular Format an easier way to do it is

    DateTime.ParseExact("date time string", format, CultureInfo.currentCulture)

    the Date time string is the string that you want.
    fo...