Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to DateTime conversion as per specified format

Tags:

c#

asp.net

I would like to have my end result in date format as per the specified format i.e YYMMDD how can i get this from a string given as below

   string s="110326";  
like image 913
Vivekh Avatar asked Jul 19 '11 12:07

Vivekh


People also ask

How do I convert a string to a datetime object in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I convert a string to a timestamp in python?

Import the datetime library. Use the datetime. datetime class to handle date and time combinations. Use the strptime method to convert a string datetime to a object datetime.

What is the format of datetime?

For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".


1 Answers

From string to date:

DateTime d = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);

Or the other way around:

string s = d.ToString("yyMMdd");

Also see this article: Custom Date and Time Format Strings

like image 132
Sven Avatar answered Oct 14 '22 14:10

Sven