C# DateTime to “YYYYMMDDHHMMSS” format

2018-12-31 05:58发布

I want to convert a C# DateTime to "YYYYMMDDHHMMSS" format. But I don't find a built in method to get this format? Any comments?

标签: c# datetime
14条回答
公子世无双
2楼-- · 2018-12-31 06:49

In .Net Standard 2 you can format DateTime like belows:

DateTime dt = DateTime.Now;
CultureInfo iv = CultureInfo.InvariantCulture;

// Default formats
// D - long date           Tuesday, 24 April 2018
// d - short date          04/24/2018
// F - full date long      Tuesday, 24 April 2018 06:30:00
// f - full date short     Tuesday, 24 April 2018 06:30
// G - general long        04/24/2018 06:30:00
// g - general short       04/24/2018 06:30
// U - universal full      Tuesday, 24 April 2018 06:30:00
// u - universal sortable  2018-04-24 06:30:00
// s - sortable            2018-04-24T06:30:00
// T - long time           06:30:00
// t - short time          06:30
// O - ISO 8601            2018-04-24T06:30:00.0000000
// R - RFC 1123            Tue, 24 Apr 2018 06:30:00 GMT           
// M - month               April 24
// Y - year month          2018 April
Console.WriteLine(dt.ToString("D", iv));

// Custom formats
// M/d/yy                  4/8/18
// MM/dd/yyyy              04/08/2018
// yy-MM-dd                08-04-18
// yy-MMM-dd ffffd           08-Apr-18 Sun
// yyyy-M-d ffffdd           2018-4-8 Sunday
// yyyy MMMM dd            2018 April 08      
// h:mm:ss tt zzz          4:03:05 PM -03
// HH:m:s tt zzz           16:03:05 -03:00
// hh:mm:ss t z            04:03:05 P -03
// HH:mm:ss tt zz          16:03:05 PM -03      
Console.WriteLine(dt.ToString("M/d/yy", iv));
查看更多
十年一品温如言
3楼-- · 2018-12-31 06:54

using C# 6.0

$"Date-{DateTime.Now:yyyyMMddHHmmss}"
查看更多
浅入江南
4楼-- · 2018-12-31 06:56
string date = DateTime.Now.ToString("dd-MMM-yy");  //05-Aug-13
查看更多
浮光初槿花落
5楼-- · 2018-12-31 06:57

I am surprised no one has a link for this . any format can be created using the guidelines here:

Custom Date and Time Format Strings

For your specific example (As others have indicated) use something like

my_format="yyyyMMddHHmmss";
DateTime.Now.ToString(my_format);

Where my_format can be any string combination of y,M,H,m,s,f,F and more! Check out the link.

查看更多
萌妹纸的霸气范
6楼-- · 2018-12-31 06:57

An easy Method, Full control over 'from type' and 'to type', and only need to remember this code for future castings

DateTime.ParseExact(InputDate, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("yyyy/MM/dd"));
查看更多
只靠听说
7楼-- · 2018-12-31 07:02

Get the date as a DateTime object instead of a String. Then you can format it as you want.

  • MM/dd/yyyy 08/22/2006
  • ffffdd, dd MMMM yyyy Tuesday, 22 August 2006
  • ffffdd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
  • ffffdd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
  • ffffdd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
  • ffffdd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
  • ffffdd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
  • MM/dd/yyyy HH:mm 08/22/2006 06:30
  • MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
  • MM/dd/yyyy H:mm 08/22/2006 6:30
  • MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
  • MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07

Click here for more patterns

查看更多
登录 后发表回答