简体   繁体   English

SQL转换dd-MM-yyyy HH:mm:SS到MM / dd / yyyy

[英]SQL convert dd-MM-yyyy HH:mm:SS to MM/dd/yyyy

How can I convert '11-09-2012 5:08:31 PM' to '09/11/2012' ? 如何将'11-09-2012 5:08:31 PM'转换为'09/11/2012' '11-09-2012 5:08:31 PM' '09/11/2012' (From dd-MM-yyyy HH:mm:SS to MM/dd/yyyy ). (从dd-MM-yyyy HH:mm:SSMM/dd/yyyy )。

Date is 11 September 2012 . 日期是11 September 2012
Or is there any way to convert this in C#? 或者有没有办法在C#中转换它? But in C# I want only date not string. 但是在C#中我只希望日期不是字符串。

Using SQL Server query: 使用SQL Server查询:

SELECT CONVERT(DATETIME, '11-09-2012 5:08:31 PM', 101);

This will convert in MM/dd/yyyy .For more info see CAST and CONVERT (Transact-SQL) and How to convert from string to datetime? 这将转换为MM/dd/yyyy 。有关更多信息,请参阅CAST和CONVERT(Transact-SQL)以及如何从字符串转换为日期时间?


To convert using C# . 要使用C#进行转换。

You can do that like this: 你可以这样做:

string myDate = Convert.ToDateTime("11-09-2012 5:08:31 PM").ToString("MM/dd/yyyy"
                                                  ,CultureInfo.InvariantCulture);

But as you don't want result in String , you can use DateTime.ParseExact method. 但是,由于您不希望结果为String ,因此可以使用DateTime.ParseExact方法。

DateTime parsedDate = DateTime.ParseExact("11-09-2012 5:08:31 PM", 
                                          "MM/dd/yyyy", 
                                          CultureInfo.InvariantCulture);

SQL SERVER : SQL SERVER:

Do like this for [MM/DD/YYYY] format: 对[MM / DD / YYYY]格式这样做:

SELECT CONVERT(VARCHAR(10), CAST('11-09-2012 5:08:31 PM' AS DATETIME), 101) AS [MM/DD/YYYY]

Similary, if you want to convert in [DD/MM/YYYY] format, you can do like this 同样,如果你想以[DD / MM / YYYY]格式转换,你可以这样做

SELECT CONVERT(VARCHAR(10), CAST('11-09-2012 5:08:31 PM' AS DATETIME), 103) AS [DD/MM/YYYY]

C# C#

In C#, you simply do like this: 在C#中,你只需这样做:

string formattedDt= Convert.ToDateTime("11-09-2012 5:08:31 PM")
                           .ToString("MM/dd/yyyy",CultureInfo.InvariantCulture);

In c# you should be able to use the method : 在c#中你应该能够使用这个方法

DateTime.ToShortDateString();

Or even: 甚至:

DateTime.Now.Date -- will produce just the date

Note from MSDN: 来自MSDN的说明:

The string returned by the ToShortDateString method is culture-sensitive. ToShortDateString方法返回的字符串对文化敏感。 It reflects the pattern defined by the current culture's DateTimeFormatInfo object. 它反映了当前文化的DateTimeFormatInfo对象定义的模式。 For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; 例如,对于en-US文化,标准的短日期模式是“M / d / yyyy”; for the de-DE culture, it is "dd.MM.yyyy"; 对于de-DE文化,它是“dd.MM.yyyy”; for the ja-JP culture, it is "yyyy/M/d". 对于ja-JP文化,它是“yyyy / M / d”。 The specific format string on a particular computer can also be customized so that it differs from the standard short date format string. 特定计算机上的特定格式字符串也可以自定义,以便它与标准短日期格式字符串不同。

string date = "11-09-2012 5:08:31 PM";
string newDate = DateTime.Parse(date).ToString("dd/MM/yyyy");

Here is the sample code using Date time parse and for more information you can visit the link C-sharp date time formats 以下是使用日期时间解析的示例代码,有关详细信息,您可以访问链接C-sharp日期时间格式

string inputString = "11/08/2012";
DateTime dt = DateTime.MinValue;
try {
dt = DateTime.Parse(inputString);    
} 
catch (Exception ex) {
// handle the exception however you like.
return;
}
string formattedDate = String.Format("{0:MMM d, yyyy}", dt);

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

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