更改日期格式 [英] Changing the format of date
本文介绍了更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DateTime变量(例如,时间戳记),其日期格式如下:
I have a DateTime variable (say, timestamp) that holds a date in its usual format like this:
11/1/2011
此变量用于构建SQL命令。 Oracle数据库仅接受以下格式的日期:
This variable is used to build a SQL command. The Oracle database only accepts dates in the format
YYYY-MM-DD
如何操作变量以这种格式存储日期?
How can I manipulate my variable to store the date in this format?
推荐答案
我假设您在SQL命令中以字符串形式发送日期。
I assume you send the date as string in the SQL command.
DateTime date = ...your object...;
string formattedDate = date.ToString("yyyy-MM-dd");
如果为字符串格式,则需要首先对其进行解析。很难从您的字符串中看到它是日/月/年还是月/日/年。
If it´s in string format, then you need to parse it first. It´s hard to see from your string if it´s day/month/year or month/day/year.
但是您可以执行以下操作:
But you could do something like this:
string sDateTime = "11/1/2011";
DateTimeFormatInfo format = new DateTimeFormatInfo();
format.ShortDatePattern = "dd/MM/yyyy"; // or MM/dd/yyyy
DateTime date = DateTime.Parse(sDateTime, format);
string formattedDate = date.ToString("yyyy-MM-dd");
这篇关于更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文