C#和SQL中的日期时间格式 [英] Date Time Format in C# and SQL
本文介绍了C#和SQL中的日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的要求是....在c#中我需要在所有服务器控件中显示日期格式为dd / MM / yyyy,但在sql中日期格式为MM / dd / yyyy如何?
My requirement is....In c# i need to show date format as "dd/MM/yyyy" in all server control, but in sql the date format is "MM/dd/yyyy" how ?
推荐答案
使用此方法以sql格式转换日期(DD / MM / yyyy)(MM / DD / yyyy)
Use this method convert Date(DD/MM/yyyy) in sql format(MM/DD/yyyy)
public static string ConvertDtFormat(string dateTimeString)
{
dateTimeString = dateTimeString.Trim();
while (dateTimeString.Contains(" "))
{
dateTimeString = dateTimeString.Replace(" ", " ");
}
if (dateTimeString == null || dateTimeString.Length == 0)
{
return string.Empty;
}
else
{
DateTime convertedDateTime = new DateTime();
string userDateFormat = HMS.DEFAULT_DATE_FORMAT;
try
{
if (userDateFormat.Trim().Contains("dd/MM/yyyy"))
convertedDateTime = DateTime.ParseExact(dateTimeString, format_dmy, CultureInfo.InvariantCulture,
DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
else if (userDateFormat.Trim().Contains("MM/dd/yyyy"))
convertedDateTime = DateTime.ParseExact(dateTimeString, format_mdy, CultureInfo.InvariantCulture,
DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
return convertedDateTime.ToString("MMM dd, yyyy hh:mm tt");
}
catch
{
return "Invalid DateTime";
}
}
}
使用DateTime
或日期
数据库中用于存储日期的数据类型。在C#中,使用DateTime
类型变量来存储日期。在C#中,使用字符串格式将日期格式化为您选择的格式。
C#字符串格式示例:
UseDateTime
orDate
data type in the database to store the date. In C#, useDateTime
type variable to store the date. In C#, use string formatting to format the date into your choice of format.
C# Example of string formatting:
Console.WriteLine(YourDateTimeVariable.ToString("MM/dd/yyyy"))
这个链接可以帮到你。
c#和sql server中的日期时间格式
This link may help you.
date time format in c# and sql server
这篇关于C#和SQL中的日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文