使用 C# DateTime 展示不同格式的年份
設定 DateTime 物件;
DateTime dt = DateTime.Now;
要獲得不同格式的年份,請嘗試以下方法 −
dt.ToString("yy")
dt.ToString("yyy")
dt.ToString("yyyy")
dt.ToString("yyyyy")以下是完整的程式碼 −
示例
using System;
using System.Threading;
using System.Diagnostics;
public class Demo {
public static void Main() {
DateTime dt = DateTime.Now;
// Today's date
Console.WriteLine("Today = {0}", DateTime.Today);
Console.WriteLine("Displaying current year in dufferent formats:");
Console.WriteLine(dt.ToString("yy"));
Console.WriteLine(dt.ToString("yyy"));
Console.WriteLine(dt.ToString("yyyy"));
Console.WriteLine(dt.ToString("yyyyy"));
}
}輸出
Today = 9/4/2018 12:00:00 AM Displaying current year in dufferent formats: 18 2018 2018 02018
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP