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).
Related articles
Related discussion
-
hey developers out there
by pitsophera (0 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
-
Creating a Windows Service in VB.NET
by davidvanr (108 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
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
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
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.
This thread is for discussions of Parse a UK Date String.