Convert DateTime to a specified Format

前提是你 提交于 2019-11-26 16:43:56

问题


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.


回答1:


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")



回答2:


Even easier way to convert Date:

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



回答3:


var dateTime = DateTime.ParseExact("12/02/21 10:56:09", "yy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);

var text = dateTime.ToString("MMM. dd, yyyy HH:mm:ss");



回答4:


Assuming that you are meaning to ask how to get VB to parse the date as yy/MM/dd, the answer is simple: just use DateTime.ParseExact("12/02/12 10:56:09", "yy/MM/dd HH:mm:ss") and then use ToString() as before.




回答5:


Try this:

DateTime.ParseExact("12/02/21 10:56:09", "yy/MM/dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).ToString("MMM. dd, yyyy HH:mm:ss");


来源:https://stackoverflow.com/questions/9371658/convert-datetime-to-a-specified-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!