1

I have tried various options for my problem but none seems to work for me. I am working in # and have a variable with datatype date with value as {07/03/2013 17:27:02}, now i want this date to be shown as 03-July-2013 but it seems to come out as 07-March-2013.

Bit of code

DateTime publishDate = "07/03/2013 17:27:02"

string publish = publishDate.ToString("dd-MMM-yyyy")

Tried this too: String.Format("{0:MMM d, yyyy}", publishDate )

This is what i am trying, i thought this would be quite simple conversion but i am struck. Can anybody point me in right direction

4
  • 2
    try String.Format("{0:dd-MMMM-yyyy}", publishDate ) Commented Jul 4, 2013 at 16:39
  • DateTime publishDate = "07/03/2013 17:27:02" will not compile. You can't just assign a string to a DateTime. Commented Jul 4, 2013 at 19:49
  • How does this relate to SQL that you put in your title? Commented Jul 4, 2013 at 19:50
  • Matt, just of simplicity i gave whats in my datetime variable. Commented Jul 4, 2013 at 19:57

5 Answers 5

3

Here is a reference for formating datetimes as strings in C#

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

You need String.Format("{0:dd-MMMM-yyyy}", publishDate ) by the looks of it.

If your date is coming out as March instead of July, then it looks like your regional format is incorrect somewhere.. That could be a problem.

Sign up to request clarification or add additional context in comments.

1 Comment

problem was, for some reason, date was being saved as 2013-dd-mm format rather than 2013-mm-dd format in database.
1

Use "MMMM" to show the full name of the month.

Console.Write(DateTime.Now.ToString("dd-MMMM-yyyy")); // Result: 04-july-2013 

Comments

1

Try this instead:

DateTime publishDate = DateTime.ParseEaxct("07/03/2013 17:27:02", "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); string publish = publishDate.ToString("dd-MMMM-yyyy"); 

Hope this will fix your issue.

Comments

0

Since your date is outputted with the month and day value inverted, I would consider using :

DateTime.ToString("dd-MMMM-yyyy, CultureInfo.InvariantCulture); 

InvariantCulture won't take your system Culture settings in consideration.

Comments

0

You can tell your database to generate the right format for every date or time stamp. This avoids any conversion errors on the client side.

This are some examples for Oracle:

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'; ALTER SESSION SET NLS_TIME_FORMAT = 'HH24:MI:SS'; ALTER SESSION SET NLS_TIME_TZ_FORMAT = 'HH24:MI:SS TZH:TZM'; ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'; ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS TZH:TZM'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.