Easy ASP.NET UK Date Formatting

When I first started coding in ASP.NET for some really stupid reason my nemesis was always formatting dates, trying to get them to to display how I wanted them to and also in a UK format (Day first, Month then Year).

After some hunting I found out how easy it actually is using the built in .ToString() method...  Below is an example function that will return any date in the UK format

Function: Just pass in a date (You can use now() or whatever you like)  

Function FormatLongDateUK(ByVal TheDate As DateTime) As String Dim s As String 'create a string s = TheDate.ToString("dd MMMM yyyy") Return s End Function

Result = 12 December 2007

Now have a play around with the ToString() method by changing the day format to some of the below

ToString("dd MM yyyy") ToString("dddd MMMM yyyy") ToString("MMMM dd, yyyy") ToString("MM dddd, yy")

As you can see its VERY easy to change the output and even switch between UK & US dates etc...   As I know how hard it is moving from ASP to ASP.NET I found this VBScript list a while back and used to use it to format dates before I started to use the above, so in case you want to stick to VBScript for a bit longer - This should help you

 

Now()

1/19/00 11:34:53 AM

IsDate(Now())

True

CDate(Now())

1/19/00 11:34:53 AM

Year(Now())

2000

Month(Now())

1

MonthName(Month(Now()))

January

Day(Now())

19

Hour(Now())

11

Minute(Now())

34

Second(Now())

53

Time()

11:34:53 AM

Hour(Now())

11

Minute(Now())

34

Second(Now())

53

TimeValue(Now())

11:34:53 AM

IsDate(TimeValue(Now()))

True

Hour(TimeValue(Now()))

11

Minute(TimeValue(Now()))

34

Second(TimeValue(Now()))

53

TimeSerial(hour(Now()),minute(Now()),second(Now()))

11:34:53 AM

IsDate(TimeSerial(hour(Now()),minute(Now()),second(Now())))

True

Hour(TimeSerial(hour(Now()),minute(Now()),second(Now())))

11

Minute(TimeSerial(hour(Now()),minute(Now()),second(Now())))

34

Second(TimeSerial(hour(Now()),minute(Now()),second(Now())))

53

DateValue(Now())

1/19/00

IsDate(DateValue(Now()))

True

WeekDay(DateValue(Now()))

4

WeekDayName(WeekDay(DateValue(Now())) )

Wednesday

Year(DateValue(Now()))

2000

Month(DateValue(Now()))

1

Day(DateValue(Now()))

19

DateSerial(Year(Now()),Month(Now()),Day(Now()))

1/19/00

WeekDay(DateSerial(Year(Now()),Month(Now()),Day(Now())))

4

WeekDay(DateSerial(Year(Now()),Month(Now()),Day(Now())),2)

3

Year(DateSerial(Year(Now()),Month(Now()),Day(Now())))

2000

Month(DateSerial(Year(Now()),Month(Now()),Day(Now())))

1

Day(DateSerial(Year(Now()),Month(Now()),Day(Now())))

19

First Day In Month: weekDayName(weekDay(DateSerial(Year(Now()),Month(Now()),1)))

Saturday

Last Day In Month: DateAdd("d",-1,DateSerial(Year(Now()),Month(Now())+1,1)))

1/31/00

DateAdd("m",-1,Now())

12/19/99 11:34:53 AM

DateAdd("h",1,Now())

1/19/00 12:34:53 PM

DateDiff("m", "1/1/99", Now())

12

formatDateTime(Now(), vbGeneralDate)

1/19/00 11:34:53 AM

formatDateTime(Now(), vbLongDate)

Wednesday, January 19, 2000

formatDateTime(Now(), vbShortDate)

1/19/00

formatDateTime(Now(), vbLongTime)

11:34:53 AM

formatDateTime(Now(), vbShortTime)

11:34

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]