简体   繁体   中英

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

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.

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

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM