Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a DateTime object to YYMMDD format?

Um, probably a really simple question, but I just noticed that I have no idea on how to convert DateTime.Now to the format YYMMDD, so for example today (5. November 2009) would be "091105".

I know there are overloads to DateTime.Now.ToString() where you can pass in a format string, but I have not found the right format e.g. for short year format (09 instead of 2009).

like image 377
Max Avatar asked Nov 05 '09 10:11

Max


People also ask

How do you convert datetime to mm dd yyyy?

For the data load to convert the date to 'yyyymmdd' format, I will use CONVERT(CHAR(8), TheDate, 112). Format 112 is the ISO standard for yyyymmdd.

What is Yymmdd format?

Acronym. Definition. YYMMDD. (2-Digit) Year, Month, and Day.

How do I format a datetime object in Python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.


3 Answers

DateTime.Now.ToString("yyMMdd")

You may also find the following two posts on MSDN useful as they contain a lot of info about DateTime formatting:

Standard Date and Time Format Strings
Custom Date and Time Format Strings

like image 124
Dave D Avatar answered Oct 22 '22 19:10

Dave D


Another alternative that you can do this is by:

var formattedDate = string.Format("{0:yyMMdd}", DateTime.Now);

Hope this helps!

like image 39
mallows98 Avatar answered Oct 22 '22 19:10

mallows98


The reference for the format string is at MSDN: Custom DateTime Format Specifiers.

What you are looking for specifically is:

yy Represents the year as a two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the year has fewer than two digits, the number is padded with leading zeroes to achieve two digits.

MM Represents the month as a number from 01 through 12. A single-digit month is formatted with a leading zero.

dd Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero.

like image 37
Guffa Avatar answered Oct 22 '22 18:10

Guffa