Library code snippets

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).

Comments

  1. 13 Aug 2006 at 00:25
    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.
    format is the way you want the String to be read dd = day, MM = month, yyyy = four digit year, yy is 2 digit year, HH = 24 Hours clock, hh = 12 hour clock,
    mm = minutes, ss = seconds.
    cultureinfo.currentculture takes on the deault culture you can change it to anything you want.

    the advantage is that it will parse the date and if it finds the date to be invalid it will raise an exception.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Parse a UK Date String.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...
AddThis

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

Want to stay in touch with what's going on? Follow us on twitter!