时间日期格式 c#_C#| 以不同格式打印日期和时间

时间日期格式 c#

Following formats are used to print date and time in different formats,

以下格式用于以不同格式打印日期和时间,

FormatDescription
d This is used to represent the day of any month, its value lies between 1 to 31.
dd This is also used to represent the day of any month, its value lies between 1 to 31.
ddd This is used to represent the name of the day in three-character like (Mon, Tue, etc).
dddd This is used to represent the full name of the day like (Monday, Tuesday, etc).
h This is used for a 12-hour clock for example 5.
hh This is used for 12-hour with left padded 0 for example 05.
H This is used for a 24-hour clock for example 14.
HH This is used for 24-hour with left padded 0 for example 05.
m This is used to represent minutes.
mm This is used to represent minutes with left padded 0 for example 07.
M This is used to represent the month number.
MM This is used to represent month number with left padded 0.
MMM This is used to represent month name in 3 characters like (Jan, Feb, etc).
MMMM This is used to represent a full month name like (January).
s This is used to represent second.
ss This is used to represent second with left padded 0.
t This is used to represent AM or PM for example(A or P).
tt This is used to represent AM or PM for example(AM or PM).
y This is used to represent the year.
yy This is used to represent year with left padded 0.
yyy This is used to represent a full-year number like 2019.
yyyy This is used to represent a full-year number like 2019.
格式 描述
d 用于表示任何月份的日期,其值介于1到31之间。
dd 这也用于表示任何月份的日期,其值介于1到31之间。
ddd 这用于以三个字符(如星期一,星期二等)表示日期的名称。
dddd 这用于表示一天的全名,例如(星期一,星期二等)。
H 例如,这用于12小时制时钟。
h 这用于12小时,左侧填充0,例如05。
H 例如,它用于24小时制时钟。
H 这用于24小时,左侧填充0,例如05。
这用来代表分钟。
毫米 这用于表示分钟,左侧填充0,例如07。
中号 用于表示月份数。
MM 用于表示月份数,左侧填充0。
MMM 这用于以3个字符(如Jan,Feb等)表示月份名称。
MMMM 这用来表示一个完整的月份名称,例如(一月)。
s 这用来代表第二。
ss 这用于表示左填充0的秒。
Ť 例如,它用于表示AM或PM(A或P)。
tt 例如,它用于表示AM或PM(AM或PM)。
ÿ 这用来表示年份。
y 这用来表示年份,左侧填充0。
y 这用于表示像2019这样的全年数字。
yyyy 这用于表示像2019这样的全年数字。

Below program shows how we can print the current date and time in different date time formats in C#?

下面的程序显示了如何在C#中以不同的日期时间格式打印当前日期和时间

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get current date and time using .Now property
            DateTime dt = DateTime.Now;

            //Now we print all possible date and time formates.
            Console.WriteLine(dt.ToString("yyyy MMMM"));
            Console.WriteLine(dt.ToString("HH:mm:ss"));
            Console.WriteLine(dt.ToString("h:mm tt"));
            Console.WriteLine(dt.ToString("H:mm"));
            Console.WriteLine(dt.ToString("hh:mm tt"));
            Console.WriteLine(dt.ToString("HH:mm"));
            Console.WriteLine(dt.ToString("ddd, dd MMM yyy HH':'mm':'ss 'GMT'"));
            Console.WriteLine(dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"));  
            Console.WriteLine(dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"));
            Console.WriteLine(dt.ToString("MMMM dd"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm:ss"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy h:mm tt"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy H:mm"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm"));
            Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
            Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy"));
            Console.WriteLine(dt.ToString("MM/dd/yyyy"));
            Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy")); 
            Console.WriteLine(dt.ToString("MM/dd/yyyy hh:mm tt"));
            
            Console.WriteLine();  
        }
    }
}

Output

输出量

2019 November
08:06:01
8:06 AM
8:06
08:06 AM
08:06
Sat, 02 Nov 2019 08:06:01 GMT
2019-11-02T08:06:01
2019-11-02T08:06:01.0862340+00:00
November 02
11/02/2019 08:06:01
11/02/2019 8:06 AM
11/02/2019 8:06
11/02/2019 08:06
Saturday, 02 November 2019 08:06:01
Saturday, 02 November 2019
11/02/2019
Saturday, 02 November 2019
11/02/2019 08:06 AM


翻译自: https://www.includehelp.com/dot-net/print-date-and-time-in-different-formats.aspx

时间日期格式 c#

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值