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?
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"));
If you use ReSharper, get help with ':' (see image)
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.
Get the date as a DateTime
object instead of a String. Then you can format it as you want.
Click here for more patterns
You've just got to be careful between months (MM) and minutes (mm):
DateTime dt = DateTime.Now; // Or whatever
string s = dt.ToString("yyyyMMddHHmmss");
(Also note that HH is 24 hour clock, whereas hh would be 12 hour clock, usually in conjunction with t or tt for the am/pm designator.)
If you want to do this as part of a composite format string, you'd use:
string s = string.Format("The date/time is: {0:yyyyMMddHHmmss}", dt);
For further information, see the MSDN page on custom date and time formats.
It is not a big deal. you can simply put like this
WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss")}");
Excuse here for I used $ which is for string Interpolation .