简体   繁体   English

在C#中将dd / MM / yyyy HH:mm tt转换为MM / dd / yyyy HH:mm tt

[英]Convert dd/MM/yyyy HH:mm tt to MM/dd/yyyy HH:mm tt in C#

I want to convert string as : "25/12/2017 4:00 PM" to "12/25/2017 4:00 PM" . 我想将字符串转换为: "25/12/2017 4:00 PM""12/25/2017 4:00 PM" My code : 我的代码:

var TDXRSC = "25/12/2017 4:00 PM";
DateTime.ParseExact(TDXRSC, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);

But it's not working. 但这不起作用。

The issue is your date format expected is dd/MM/yyyy hh:mm tt but the reference date only has a single digit hour 4 . 问题是您的日期格式应该是dd/MM/yyyy hh:mm tt但是参考日期只有一个数字小时4 You are probably better off not expect leading zeros for days, months or hours. 您可能最好不要在几天,几个月或几小时内都领先于零。

Try.. 尝试..

var TDXRSC = "25/12/2017 4:00 PM";
var input = DateTime.ParseExact(TDXRSC, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);

This will also still parse 2 digit hours. 这也将解析2位数小时。 So var TDXRSC = "25/12/2017 12:00 PM"; 因此var TDXRSC = "25/12/2017 12:00 PM"; will still parse correctly. 仍然可以正确解析。

var TDXRSC = "25/12/2017 4:00 PM";
var input = DateTime.ParseExact(TDXRSC, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);
var output = input.ToString("MM/dd/yyyy h:mm tt");

When you call ParseExact you're telling the compiler what format the incoming date is. 当您调用ParseExact您是在告诉编译器传入日期是什么格式。 You can then use ToString() method to provide a format for a string representation of the parsed date. 然后,您可以使用ToString()方法来提供解析日期的字符串表示形式的格式。

Hope that .TryParseExtract will be more safe to use for conversion, use like the following: 希望.TryParseExtract可以更安全地进行转换,使用方式如下:

var dateString = "25/12/2017 4:00 PM";
DateTime inputDate; 
if(DateTime.TryParseExact(dateString, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDate))
{
   var output = inputDate.ToString("MM/dd/yyyy hh:mm tt");
   Console.WriteLine(output);
}
else
{
     Console.WriteLine("Conversion failed");
}

Working Example 工作实例

var TDXRSC = "25/12/2017 4:00 PM";
DateTime date = Convert.ToDateTime(TDXRSC);
string Format = date.ToString("MM/dd/yyyy h:mm tt");

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

相关问题 将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt - Convert string to Datetime dd/MM/yyyy hh:mm:ss tt 将属性值转换为yyyy-MM-dd hh:mm:ss tt格式 - Convert the property value to yyyy-MM-dd hh:mm:ss tt format 将类似“ mm / dd / yyyy hh:mm:ss tt”的字符串转换为DateTime - Convert a string like “mm/dd/yyyy hh:mm:ss tt” to DateTime 将格式为(yyyy-mm-dd HH:mm:ss + TT:TT)的DateTimes插入SQL Server - Insert DateTimes of format (yyyy-mm-dd HH:mm:ss +TT:TT) into SQL Server 如何在linq C#中比较日期时间(只有dd / mm / yyyy hh:mm:ss tt不是datetime中的整个fff部分)? - How to compare datetime (only dd/mm/yyyy hh:mm:ss tt not whole fff section in datetime) in linq C#? 如何在 C# 中将“YYYY-MM-DDThh:mmTZD”转换为 yyyy-MM-dd hh:mm:ss - How to convert “YYYY-MM-DDThh:mmTZD” to yyyy-MM-dd hh:mm:ss in C# 将dd / mm / yyyy hh:mm am / pm转换为MM / dd / yyyy hh:mm am / pm - Convert dd/MM/yyyy hh:mm am/pm to MM/dd/yyyy hh:mm am/pm 将日期时间显示为MM / dd / yyyy HH:mm格式c# - Display datetime into MM/dd/yyyy HH:mm format c# 在 razor 视图 c# 上将日期转换为“MM/DD/YYY hh:mm:ss tt” - Convert Date to 'MM/DD/YYY hh:mm:ss tt' on razor view c# 在 c# LINQ 中比较日期时间“MM/dd/yyyy hh:mm:ss”和“yyyy-MM-dd hh:mm:ss” - Compare dateTime "MM/dd/yyyy hh:mm:ss" with "yyyy-MM-dd hh:mm:ss" in c# LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM