Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DateTime to a specified Format

Tags:

I have this date format yy/MM/dd HH:mm:ss ex: 12/02/21 10:56:09. The problem is, when i try to convert it to different format using this code:

CDate("12/02/21 10:56:09").ToString("MMM. dd, yyyy HH:mm:ss")

It displays Dec. 12, 2021 10:56:09.

How can i correctly format it to: Feb. 21, 2012 10:56:09? This format is returned when i check balance inquiry fro my SMS based application.

like image 373
John Woo Avatar asked Feb 21 '12 03:02

John Woo


People also ask

Can we change the format of datetime 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.


2 Answers

Use DateTime.ParseExact, e.g.:

DateTime.ParseExact("12/02/21 10:56:09", "yy/MM/dd HH:mm:ss", 
    CultureInfo.InvariantCulture
    ).ToString("MMM. dd, yyyy HH:mm:ss")
like image 54
Kirill Polishchuk Avatar answered Oct 06 '22 02:10

Kirill Polishchuk


Even easier way to convert Date:

Convert.ToDateTime("12/02/21 10:56:09").ToString("MMM.dd,yyyy HH:mm:ss");
like image 31
Mitesh Vora Avatar answered Oct 06 '22 01:10

Mitesh Vora