转换日期c#

本文关键字:日期 转换 | 更新日期: 2023-09-27 17:49:39

我有一个日期,格式如下:

20/01/2011 7:15:28 PM

我需要把它转换成类似的东西

2011-01-20 09:24:06

我该怎么做?

提前谢谢。

转换日期c#

DateTime.ParseExact("20/01/2011 7:15:28 PM",
                    "dd/MM/yyyy h:mm:ss tt",
                    CultureInfo.InvariantCulture)
        .ToString("yyyy-MM-dd HH:mm:ss")

最明确的方法是:

DateTime.ParseExact("20/01/2011 7:15:28 PM", "dd/MM/yyyy h:m:s tt",
    CultureInfo.InvariantCulture).ToString("yyyy-MM-dd HH:mm:ss")

首先,您需要解析原始日期(假设您还没有将其作为DateTime(然后您需要对其进行格式化,请参阅c#中的日期格式化

DateTime time = DateTime.Now;              // Use current time
string format = "yyyy-MM-dd HH:mm:ss";    // Use this format
time.ToString(format);  // Write to console

格式化

d
Use this to specify the numeric value for the day of the month. It will be one or two digits long.
dd
This is the same as a single d, except there are always two digits, with a leading 0 prepended if necessary.
ddd
This displays a three-letter string that indicates the current day of the week.
dddd
This displays the full string for the day of the week.
f
ff
fff
ffff
fffff
ffffff
fffffff
F
FF
FFF
FFFF
FFFFF
FFFFFF
FFFFFFF
Use the lowercase f to indicate the seconds to one digit length. Use two lowercase fs to indicate the seconds to two digits. The uppercase F patterns do the same but work differently on trailing zeros.
gg
Use this to display A.D. on your date. It is unlikely that this will be B.C. in most programs.
h
Display the hours in one digit if possible. If the hours is greater than 9, it will display two digits. Range is 1-12.
hh
Display the hours in two digits always, even if the hour is one digit. The range here will be 01-12.
H
This represents the hours in a range of 0-23, which is called military time in some parts of the world.
HH
This represents the hours in a range of 00-23. The only different here between the single H is that there is always a leading zero if the number is one digit.
K
Use this to display time zone information.
m
mm
This formats the minutes in your date format string. Here, the one m means that there is only one digit displayed if possible. The two ms means that there are always two digits displayed, with a leading zero if necessary.
M
MM
These display the months in numeric form. The one uppercase M does not have a leading zero on it. The two uppercase Ms will format a number with a leading zero if it is required.
MMM
This displays the abbreviated three-letter form of the month represented in the DateTime.
MMMM
This displays the full month string, properly capitalized.
s
ss
The lowercase s displays seconds. A single lowercase s means that you do not require a leading zero. Two lowercase s characters means you always want two digits, such as 00-59.
t
Use the lowercase t to indicate A, when the time is in the AM, and P, for when the time is in PM.
tt
Use two lowercase tts to display the full AM or PM string. You will normally want this for when you are displaying the string to a user.
y
yy
yyy
yyyy
yyyyy
These display the year to different digits. In your programs, you won't need three digits for the year, or five. Therefore, you should only consider one y, two ys, or four ys.
z
zz
zzz
These represent the offset from the UTC time on the local operating system.
:
This is the time separator.
/
This is the date separator.

问候

检查此项:

String Mydate = "20/01/2011 7:15:28 PM";
String result = DateTime.Parse(MyDate).ToString("yyyy-MM-dd HH:mm:ss") 
MessageBox.Show(result);

问候