简体   繁体   English

如何将SQL日期转换为可读格式

[英]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 . 我正在#中工作,并且有一个数据类型为date的变量,其值为{07/03/2013 17:27:02} ,现在我希望将此日期显示为03-July-2013但它看起来像是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# 这是在C#中将日期时间格式化为字符串的参考

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

You need String.Format("{0:dd-MMMM-yyyy}", publishDate ) by the looks of it. 根据其外观,您需要String.Format(“ {0:dd-MMMM-yyyy}”,publishDate)。

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. 如果您的日期是3月而不是7月,则您的区域格式在某处似乎不正确。这可能是个问题。

Use "MMMM" to show the full name of the month. 使用“ MMMM”显示月份的全名。

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. InvariantCulture不会考虑您的系统文化设置。

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: 这是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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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