• Grumpy Wookie

  • Archives

  • Top Posts

  • Recent Visitors

  • Blog Stats

    • 347,676 hits

Formatting Dates (C#)

When coding within C#, I’m always off to BING or GOOGLE to check on the specific syntax for different date formats – day, month, year, hour, minute, etc.

Just wanted to let you know about a GREAT link that helped me out – and wanted to blog it for future reference :

www.csharp-examples.net

As an example of what I was wanting to achieve – I was needing to display a date/time range – but only show the “EndDate” if not on the same day – something like this :

24 Sep 2009, 8:00am – 10:00am

24 Sep 2009, 8:00am – 26 Sep 2009 5:00pm

So – here’s the code I used.

============================

[ for the same “day” – just show the time – need “am” and “pm” in lower case]

** NB.  dateStart and dateEnd are both “DateTime” types

String.Format("{0:dd MMMM yyyy}", dateStart)

+ ", " + String.Format("{0:h:mmtt}", dateStart).ToLower()

+ " – " + String.Format("{0:h:mmtt}", dateEnd).ToLower();

[ for “multi day” event – include the date-to and time ]

String.Format("{0:dd MMMM yyyy}", dateStart)

+ ", " + String.Format("{0:h:mmtt}", dateStart).ToLower()

+ " – " + String.Format("{0:dd MMMM yyyy}", dateEnd)

+ ", " + String.Format("{0:h:mmtt}", dateEnd).ToLower();

============================

Here’s the link to the article I used – thanks !!

String Format for DateTime [C#]

This example shows how to format DateTime using String.Format method. All formatting can be done also using DateTime.ToString method.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: