Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  105 Posts | 1 Stories | 502 Comments | 65 Trackbacks

News



Archives

ASP.NET Ventures


*******************
Note: view the updated entry below:

http://www.codeasp.net/blogs/vivek_iit/microsoft.net/14/datetime-in-c-different-formats

*******************


Here are a few handy string formats to convert a given datetime to custom formats:

//save today's date in a string
string date = DateTime.Now.Date;

1. Show Date as " Friday 13 August 2003"

 date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.Year.ToString()

2. Show date as "Friday 13 August 13:00:13"  //time in 24H format

 date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.ToString("HH:mm:ss")

3. Show date in 12H format with AM/PM as: "Friday 13 August 01:00:13 PM"

  date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.ToString("hh:PM:mm:ss:tt")
posted on Thursday, June 14, 2007 4:11 PM

Feedback

# re: DateTime in C#: Different Formats 6/14/2007 10:53 PM Kent Sharkey
Most of those can be simplified by including all the parameters in the ToString call. For example, #2 could be replaced with:

d.ToString("dddd dd MMMM HH:mm:ss")

Apart from the conversion of data types that would possibly cause an error in the code above (you should use '&' when concatenating strings, and some of those methods return numerics), the resulting IL is also much longer with your methods.


# re: DateTime in C#: Different Formats 6/14/2007 11:04 PM Vivek
Kent,

I agree with you but sometimes we need to add extra text in between, like Friday 13th of August 2007 published at 17:00.

That's why for simplicity I used the above methods.

Good points anyways!

Cheers,

Vivek

# re: DateTime in C#: Different Formats 9/18/2007 12:53 PM hilm
Cannot implicitly convert type 'System.DateTime' to 'string'

why cant i use this code?

string date = DateTime.Now.Date;

# re: DateTime in C#: Different Formats 11/22/2007 8:51 AM winlonghorn
Because you didn't create a new instance of the DateTime Object when you declared it. :) You would have to put the "new" keyword before DateTime.Now.Date. :)


# re: DateTime in C#: Different Formats 2/27/2008 12:22 AM Tom
how do we display with a format of dd-mmm-yyyy in c#?How do we assign a date value to a date variable (eg:in vb.. dt = #12/31/9999#)?

# re: DateTime in C#: Different Formats 2/28/2008 12:09 AM Vivek Thakur
Use: Now.ToString("dd MMM yyyy")

for the second part, you need to convert to datetime object.

# re: DateTime in C#: Different Formats 3/19/2008 8:25 PM Joe
Object to Military Time:
txtMilitaryTime.Text = Convert.ToDateTime(objCrash.MilitaryDateTime).ToString("HH:mm");

# re: DateTime in C#: Different Formats 5/29/2008 11:33 PM Neco
There is one good method to show only date from DateTime object:


System.DateTime.Today.ToShortDateString()


# re: DateTime in C#: Different Formats 6/11/2008 10:48 PM noname
date.ToString("hh:PM:mm:ss:tt") it's date.ToString("hh:mm:ss:tt") instead

# re: DateTime in C#: Different Formats 6/11/2008 10:48 PM noname
date.ToString("hh:PM:mm:ss:tt") it's date.ToString("hh:mm:ss:tt") instead

# re: DateTime in C#: Different Formats 7/14/2008 1:08 PM Chris
I'm trying to send a datetime object through in the format "yyyy/MM/dd" without the time display using the following code:
DateTime tDate = DateTime.Now.Date;
String tempDate = tDate.ToString("yyyy/MM/dd");
tDate = DateTime.Parse(tempDate);
When I display the object it displays the time a again. Is there anyway to only send the date without adding the time?

# re: DateTime in C#: Different Formats 8/9/2008 4:27 AM Paul Hayman
Also check out : http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

lists a bunch of formats

# re: DateTime in C#: Different Formats 9/5/2008 10:07 AM Kedar Godkhindi
For the following code snippet, i am getting date along with time. Is there any way to get only the date, ie. dd mm yy format.

DateTime tDate = DateTime.Now.Date;
lblDate.Text = tDate.ToString();



# re: DateTime in C#: Different Formats 9/12/2008 4:33 AM Akshroon
Vivek you can use the String.Format along with the additional wording you want to add like Kent suggested. Much cleaner that way. I am not sure if the ToString method works the same way but String.Format does the job without doing all those concatenations that you've shown.

# re: DateTime in C#: Different Formats 9/18/2008 6:22 PM Rath
how can i just display a military time?

# re: DateTime in C#: Different Formats 12/13/2008 3:47 PM Vipul Patel
We can Use Different Formats like,

DateTime.Now.ToString("MM/dd/yyyy");

DateTime.Now.ToString("MMM dd yyyy");

DateTime.Now.ToString("dd/MM/yyyy");




# re: DateTime in C#: Different Formats 12/18/2008 2:14 PM K Shiva Prakash
Hi guys, i need to convert the string "20081230" to date format. Anybody know how to do it in c#

# re: DateTime in C#: Different Formats 12/18/2008 4:00 PM Govin
shiva you can use the following code,

string fetch = "20081230";
string fetchfinal = fetch.Substring(4, 2) + "/" + fetch.Substring(6, 2) + "/" + fetch.Substring(0, 4);
DateTime date= DateTime.Parse(fetchfinal);


# re: DateTime in C#: Different Formats 2/3/2009 5:18 PM krishna
I'm trying to send a datetime object through in the format "yyyy/MM/dd" without the time display using the following code:
DateTime tDate = DateTime.Now.Date;
String tempDate = tDate.ToString("yyyy/MM/dd");
tDate = DateTime.Parse(tempDate);
When I display the object it displays the time a again. Is there anyway to only send the date without adding the time?

i am also searching for the same solution.if any one know how to remove time from datetime without converting string.if we convert to string i need to convert that string again to variable of datatype DateTime.if we parse to DateTime the time showing again..plz help me ..

# re: DateTime in C#: Different Formats 5/20/2009 11:37 PM person
DateTime.Now.Date.DayOfWeek + " " + DateTime.Now.Date.Day + " " + DateTime.Now.Date.ToString("MMMM") + " " + DateTime.Now.Date.ToString("hh:mm:ss:tt")

works fine without any errors.

# re: DateTime in C#: Different Formats 9/1/2009 1:07 AM Chandralekha
Thank You for sharing your Knowledge...

# re: DateTime in C#: Different Formats 10/14/2009 11:10 PM Geo
Hello All,

I've been wanting to convert my draw time string from EST to PST? Anyone know how to do this? Thank you!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: