Question

How to convert date to 'ccyymmddhhmmss' format in c#?

Was it helpful?

Solution

You might want to try this... I don't know if cc is included, so I solved for the cc.

DateTime time = DateTime.Now;
string format = "yyMMddhhmmss";
Console.WriteLine(((Convert.ToInt32(time.ToString("yyyy")) / 100) + 1).ToString() + time.ToString(format));

For "yyMMddhhmmss".....Try this...And don't forget that capital M is Month and lower case m is minutes.

DateTime dt = Convert.ToDateTime("8 Oct 10 19:00");
Console.WriteLine(dt.ToString("yyMMddhhmmss"));

OTHER TIPS

From what I understand from your question, you want to format a c# date object to the specified format?

The easiest way to do that is by using the date.ToString("yyyyMMddHHmmss") - where date is the Date Object... There are several choices to this - like having 12-hour instead of 24-hour etc. The best option is to read through http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx and set what you need.

Hope this helps.

@Chris_techno25: I took the freedom to extend your answer:

If we stick to the question of Narashima, he wants the format ccyymmddhhmmss. So I've scratched up this extension method:

public static string IncludeCentury(this DateTime sourceDate, bool replace)
{
  var source = String.Format("{0}/{1}", sourceDate.Year / 100 + 1, sourceDate);
  if(replace)
    return Regex.Replace(source, "[^0-9]", "");
  else
    return source;
}

Usage:

var includingCentury = DateTime.Now.IncludeCentury(true)
var includingCentury = DateTime.Now.IncludeCentury(false)

Output:

21218201491410
21/2/18/2014 9:18:10 AM
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top