Jay
Jay

Reputation: 1039

How to convert SQL date into readable format

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

Upvotes: 1

Views: 1060

Answers (5)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

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.

Upvotes: 1

ceving
ceving

Reputation: 23794

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';

Upvotes: 0

ncourcy84
ncourcy84

Reputation: 101

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.

Upvotes: 0

Sam
Sam

Reputation: 7303

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.

Upvotes: 3

Vismari
Vismari

Reputation: 745

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

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

Upvotes: 1

Related Questions