C#實(shí)現(xiàn)將日期格式化為指定格式
一、DateTime.Now.ToString方法
調(diào)用DateTime對(duì)象的ToString方法可以將當(dāng)前DateTime對(duì)象的值轉(zhuǎn)換為其等效的字符串表示形式,而且ToString方法提供了重載,可以在ToString方法中添加不同的參數(shù),進(jìn)而將時(shí)間轉(zhuǎn)換為各種格式的字符串表示形式。
語(yǔ)法格式如下:
DateTime.Now.ToString("yyyy "); DateTime.Now.ToString("MM"); DateTime Now.ToString("dd"); DateTime.Now.ToString("HH"); Date Time.Now.ToString("mm"); DateTime.Now.ToString("ss");
參 數(shù) | 描 述 | 參 數(shù) | 描 述 |
yyyy | 年份,范圍為0000~9999 | HH | 時(shí),范圍為00~23 |
MM | 月份,范圍為01~12 | mm | 分鐘,范圍為00~59 |
dd | 日期,范圍為01~31 | ss | 秒,范圍為00~59 |
二、實(shí)例
// DateTime.Now.ToString() // 使用指定格式的字符串變量格式化日期字符串 namespace _061 { public partial class Form1 : Form { private GroupBox? groupBox1; private Label? label1; private Button? button1; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // button1 // button1 = new Button { Location = new Point(103, 22), Name = "button1", Size = new Size(75, 23), TabIndex = 0, Text = "開(kāi)始轉(zhuǎn)化", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // label1 // label1 = new Label { AutoSize = true, ImageAlign = ContentAlignment.TopLeft, Location = new Point(6, 45), Name = "label1", Size = new Size(56, 17), TabIndex = 1, Text = "顯示內(nèi)容" }; // // groupBox1 // groupBox1 = new GroupBox { Location = new Point(12, 12), Name = "groupBox1", Size = new Size(300, 183), TabIndex = 0, TabStop = false, Text = "日期格式轉(zhuǎn)指定格式" }; groupBox1.Controls.Add(label1); groupBox1.Controls.Add(button1); groupBox1.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(324, 206); Controls.Add(groupBox1); Name = "Form1"; StartPosition = FormStartPosition.CenterParent; Text = "將日期格式轉(zhuǎn)化為指定的格式"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); } /// <summary> /// DateTime.Now.ToString() /// 使用指定格式的字符串變量格式化日期字符串 /// </summary> private void Button1_Click(object? sender, EventArgs e) { label1!.Text = DateTime.Now.ToString("d") + "\n" + DateTime.Now.ToString("D") + "\n" + DateTime.Now.ToString("f") + "\n" + DateTime.Now.ToString("F") + "\n" + DateTime.Now.ToString("g") + "\n" + DateTime.Now.ToString("G") + "\n" + DateTime.Now.ToString("R") + "\n" + DateTime.Now.ToString("y") + "\n" + "當(dāng)前系統(tǒng)時(shí)間為:" + DateTime.Now.ToString(//使用自定義格式格式化字符串 "yyyy年MM月dd日 HH時(shí)mm分ss秒"); } } }
到此這篇關(guān)于C#實(shí)現(xiàn)將日期格式化為指定格式的文章就介紹到這了,更多相關(guān)C#日期格式化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
這篇文章主要介紹了C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法,涉及C#實(shí)現(xiàn)HTML、WORD及PDF等文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-09-09winform攔截關(guān)閉按鈕觸發(fā)的事件示例
這篇文章主要介紹了c# winform攔截關(guān)閉按鈕觸發(fā)的事件示例,大家參考使用吧2014-01-01C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法
本文主要介紹了C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08