如何以mm/dd/yyyy格式从服务器获取日期 [英] How to get date from server in mm/dd/yyyy format

查看:79
本文介绍了如何以mm/dd/yyyy格式从服务器获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用存储过程从sql server获取日期,例如:

I am taking date from sql server using stored procedure like:

DataTable getTodayDate = dal.GetTodayDate("SP_GetTodayDate");
string date = getTodayDate.Rows[0][0].ToString();
DateTime TodayDate =Convert.ToDateTime(date);


我从07/26/2012格式的sql server中获取日期.但是在转换日期"时
它正在更改为系统日期格式.但是我只需要mm/dd/yyyy格式的日期


I am getting datefrom sql server in 07/26/2012 format.But while converting "date"
It is changing to system date format.but I need date in mm/dd/yyyy format only

推荐答案

尝试以下操作:
Try this:
1 .String.Format("{0:dd/MM/yyyy}", DateTime.Now)
26/07/2012

2. String.Format("{0:MM/dd/yyyy}", DateTime.Now)
 07/26/2012

3 . String.Format("{0:dd/MM/yy}", DateTime.Now)
 26/09/12

4. String.Format("{0:t}", DateTime.Now)
 10:30 AM

5.String.Format("{0:F}", DateTime.Now)
Saturday, July 26, 2012 10:36:25 AM


..另请参阅 SQL Server日期格式语言 [ http://msdn.microsoft .com/en-us/library/5hh873ya.aspx [ ^ ].

另请参阅类System.Globalization.DateTimeFormatInfo和提供的示例代码:
http://msdn.microsoft.com/en-us/library/system.globalization. datetimeformatinfo.aspx [ ^ ],
特别要注意示例代码和类中的"SortableDateTimePattern" .


..also refer SQL Server Date Format Language[^]

Have a look on MSDN examples with some code snippets which will surely solve your problem: http://msdn.microsoft.com/en-us/library/5hh873ya.aspx[^].

Also refer the class System.Globalization.DateTimeFormatInfo and the sample code provided:
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx[^],
in particular, pay attention for "SortableDateTimePattern" in the sample code and the class.


首先,如果您实际上将日期存储为DB中的日期,字符串,请立即停止执行操作,而改用DateTime字段.如果您要从存储过程中将日期作为基于字符串的数据表返回,则也不要这样做-当数据库访问代码可以直接返回单个值(可能是DateTime)时,这是一种非常低效的方式.

然后,当您在C#代码中的DateTime中拥有值时,就不会以任何可读格式存储该值-自任意时间点以来,该值以微秒数存储.当您想要将其用于显示目的时,可以通过使用ToString方法指定格式字符串将其格式化为所需的任何顺序. (请参阅格式化显示日期时间-格式字符串说明 [ ^ ])).
如果ytou未指定格式字符串,则结果字符串的格式取决于PC上的设置-如果您设置为美国日期,则它将自动变为"MM/dd/yyyy".如果您将其设置为欧洲",则为"dd/MM/yyyy"
First off, if you are actually storing the date in you DB as a string, stop doing that right now, and use a DateTime field instead. If you are returning the date as a string based datatable from your stored procedure, then stop doing that as well - it is a very inefficient way to do it, when the DB access code can return a single value directly, which could be a DateTime.

Then, when you have the value in a DateTime in your C# code, it is not stored in any readable format - it is stored as a number of microseconds since an arbitrary point in time. When you wan to use it for display purposes, you can format it to any order you need by specifying a format string with the ToString method. (See Formatting a DateTime for display - format string description[^] for details).
If ytou don''t specify a format string, then the resulting string is in a format which depends on the settings on the PC - if you are set to US dates, then it will automatically become "MM/dd/yyyy". If you are set to European, it will be "dd/MM/yyyy"


这篇关于如何以mm/dd/yyyy格式从服务器获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆