简体   繁体   English

将.ToDateTime('Datestring')转换为日期所需的dd-MM-yyyy格式

[英]Convert.ToDateTime('Datestring') to required dd-MM-yyyy format of date

I have string of Date which can be in any format of date but I wanted to convert it to dd-MM-yyyy format. 我有Date的字符串,可以是任何格式的日期,但我想将其转换为dd-MM-yyyy格式。
I have tried every Convert.ToDatetime option which converts only to the System format. 我已经尝试了每个Convert.ToDatetime选项,它只转换为系统格式。 I want it to convert dd-MM-yyyy format. 我想要它转换dd-MM-yyyy格式。

Please reply. 请回复。 Thanks in Advance. 提前致谢。

Try this 尝试这个

DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));

This will return DateTime with your Format 这将使用您的Format返回DateTime

Despite there being many answers and this being a duplicate answer, here are some possible solutions: 尽管有很多答案,这是一个重复的答案,这里有一些可能的解决方案:

string formatted = date.ToString("dd-MM-yyyy");

or 要么

string formatted = date.ToString("dd MM yyyy");

This link might help you with more formats and options. 此链接可能会为您提供更多格式和选项。

DateTime.ParseExact has an overloaded method to accepts multiple formats, include (all)possible formats you receive and parse the string. DateTime.ParseExact有一个重载方法,可以接受多种格式,包括(所有)可能接收的格式并解析字符串。 Once you get the valid DateTime you could apply desired format in converting to string. 获得有效的DateTime您可以在转换为字符串时应用所需的格式。

  // ex...
  string dateString = ...;  // your date.

  string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                     "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                     "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                     "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                     "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
                     "MM/d/yyyy HH:mm:ss.ffffff" };

   var date = DateTime.ParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None);

  //convert to desired format.
  var strDate = date.ToString("dd-MM-yyyy");

Here we go: 开始了:

DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "/" + time.Month + "/" + time.Year);

//return: 22/05/2016 //返回:22/05/2016

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

相关问题 Convert.ToDateTime('Datestring') 到所需的 dd-MMM-yyyy 日期格式 - Convert.ToDateTime('Datestring') to required dd-MMM-yyyy format of date 如何告诉convert.todatetime输入格式为dd-mm-yyyy - how to tell convert.todatetime that the input is in format dd-mm-yyyy C#Convert.ToDateTIme函数是否将日期读为“dd / mm / yyyy”或“mm / dd / yyyy”? - Does the C# Convert.ToDateTIme function read date as “dd/mm/yyyy” or “mm/dd/yyyy”? 将日期转换为dd-mm-yyyy格式 - Convert date into the format dd-mm-yyyy 具有日期格式dd / MM / yy的DateTime.TryParseExact或Convert.ToDateTime()的范围是多少 - what is the range of DateTime.TryParseExact or Convert.ToDateTime() having date format dd/MM/yy 如何在 C# 中将日期格式转换为 DD-MM-YYYY - How to convert date format to DD-MM-YYYY in C# 如何在C#中执行Convert.ToDateTime或解析日期,但在C#2.0中仅获得dd / mm / yyyy? - How to do Convert.ToDateTime or parse date in C# but only get dd/mm/yyyy in C# 2.0? 尝试以(yyyy-mm-dd hh:mm:ss)日期格式转换字符串格式(dd-mm-yyyy hh:mm:ss) - Trying to convert string format (dd-mm-yyyy hh:mm:ss) in (yyyy-mm-dd hh:mm:ss) date format 将HH:mm:ss,dd-MM-yyyy从字符串转换为日期格式 - Convert HH:mm:ss,dd-MM-yyyy from string to date format 以“dd-mm-yyyy”格式强制JObject到serialzie日期 - Force JObject to serialzie date in “dd-mm-yyyy” format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM