C# DateTime to YYYYMMDDHHMMSS format
DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive
How to get DateTime in yyyymmddhhmmss
You can use
string strDate = DateTime.Now.ToString("yyyyMMddhhmmss");
If 24hr format is required that use uppercase HH
inplace of hh
in the format string.
Remember the first MM
should be in upper case as lower case mm
is for minutes where as uppercase is for Month.
For your particular case instead of writing a new method you can do:
new XAttribute("SLOTTINGTIME",slottingmessage.SlottingDateTime.ToString("yyyyMMddhhmmss")),
One more thing to add: The output will contain Hour in 12 hours format because of the lower case hh
part in the string. Not really sure if you need that because without AM/PM this can't indicate the accurate time. For that purpose use HH
for hours which will display hours in 24 hour format. So your code could be:
new XAttribute("SLOTTINGTIME",slottingmessage.SlottingDateTime.ToString("yyyyMMddHHmmss")),
//^^ for 24 hours format
How to check if a string in yyyyMMddHHmmss format in C#?
TryParse
doesn't have overload to provide exact format, try using TryParseExact
. Example:
// adjust IFormatProvider and DateTimeStyles if needed
if (DateTime.TryParseExact(
timestampValue,
"yyyyMMddHHmmss", //format
CultureInfo.CurrentCulture,
DateTimeStyles.None, out outDate))
{
//do work
}
Convert String value format of YYYYMMDDHHMMSS to C# DateTime
Define your own parse format string to use.
string formatString = "yyyyMMddHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
In case you got a datetime having milliseconds, use the following formatString
string format = "yyyyMMddHHmmssfff"
string dateTime = "20140123205803252";
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);
Thanks
Convert DateTime to string with format YYYYMMDD
date.ToString("yyyyMMdd");
Should be what you need.
Format DateTime.Now to yyyy-mm-dd
According to msdn MM
format specifier stands for month and mm
- for minutes.
"mm" | The minute, from 00 through 59.
"MM" | The month, from 01 through 12.
So your code should look like the following:
var dateString1 = DateTime.Now.ToString("yyyyMMdd");
var dateString2 = DateTime.Now.ToString("yyyy-MM-dd");
Console.WriteLine("yyyyMMdd " + dateString1);
Console.WriteLine("yyyy-MM-dd "+ dateString2);
And you will get the desired result
Invalid date format error yyyymmddhhmmss in c#
use this yyyyMMddHHmmss
. Because it's case sensitive
How to convert DateTime to/from specific string format (both ways, e.g. given Format is yyyyMMdd)?
if you have a date in a string with the format "ddMMyyyy" and want to convert it to "yyyyMMdd" you could do like this:
DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy",
CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
Related Topics
How to Await an Event Instead of Another Async Method
No Connection Could Be Made Because the Target Machine Actively Refused It
Using New Unity Videoplayer and Videoclip API to Play Video
String.Replace (Or Other String Modification) Not Working
Calling the Base Constructor in C#
Finding the Variable Name Passed to a Function
How to Enable External Request in Iis Express
Input String Was Not in a Correct Format
Simple Insecure Two-Way Data "Obfuscation"
Direct Casting VS 'As' Operator
C# Datetime to "Yyyymmddhhmmss" Format
How to Get the Type of T from a Member of a Generic Class or Method
Create a Strongly Typed C# Object from Json Object With Id as the Name
In C#, Should I Use String.Empty or String.Empty or "" to Intitialize a String