C# 獲取指定格式時間字符串的方法匯總
在 C# 中獲取指定格式的時間字符串有多種方式,以下是最常用的幾種方法:
1?? 使用 DateTime 的 ToString() 方法
DateTime now = DateTime.Now;
// 標(biāo)準(zhǔn)格式字符串
string shortDate = now.ToString("d"); // 2023/11/15
string longDate = now.ToString("D"); // 2023年11月15日
string shortTime = now.ToString("t"); // 14:30
string longTime = now.ToString("T"); // 14:30:45
string fullDateTime = now.ToString("F"); // 2023年11月15日 14:30:45
string sortableDateTime = now.ToString("s"); // 2023-11-15T14:30:45
string universalSortable = now.ToString("u"); // 2023-11-15 14:30:45Z
string roundTripDateTime = now.ToString("o"); // 2023-11-15T14:30:45.1234567+08:00
// 自定義格式字符串
string custom1 = now.ToString("yyyy-MM-dd"); // 2023-11-15
string custom2 = now.ToString("yyyy/MM/dd HH:mm:ss"); // 2023/11/15 14:30:45
string custom3 = now.ToString("yyyy年MM月dd日 dddd"); // 2023年11月15日 星期三
string custom4 = now.ToString("M/d/yyyy h:mm tt"); // 11/15/2023 2:30 PM
string custom5 = now.ToString("yyyy-MM-ddTHH:mm:ss.fff"); // 2023-11-15T14:30:45.1232?? String.Format 或 字符串插值
DateTime now = DateTime.Now;
// String.Format 方式
string formatted1 = string.Format("{0:yyyy-MM-dd}", now);
string formatted2 = string.Format("{0:yyyy年MM月dd日 HH時mm分ss秒}", now);
// C# 6.0+ 字符串插值方式
string formatted3 = $"{now:yyyy-MM-dd HH:mm:ss}";
string formatted4 = $"{now:yyyy年MM月dd日 ddd HH:mm}";3?? DateTimeOffset (帶時區(qū)信息)
DateTimeOffset offsetNow = DateTimeOffset.Now;
// 帶時區(qū)信息的格式
string offset1 = offsetNow.ToString("yyyy-MM-dd HH:mm:ss zzz"); // 2023-11-15 14:30:45 +08:00
string offset2 = offsetNow.ToString("o"); // ISO 8601格式 2023-11-15T14:30:45.1234567+08:004?? 常用格式模式說明
| 格式模式 | 說明 | 示例 |
|---|---|---|
| yyyy | 四位年份 | 2023 |
| MM | 兩位月份 | 11 (November) |
| dd | 兩位日期 | 05 |
| HH | 24小時制的小時 | 14 (2 PM) |
| hh | 12小時制的小時 | 02 (2 PM) |
| mm | 分鐘 | 30 |
| ss | 秒鐘 | 45 |
| fff | 毫秒 | 123 |
| tt | AM/PM | PM |
| ddd | 星期縮寫 | 周三/Wed |
| dddd | 星期全稱 | 星期三/Wednesday |
| zzz | 時區(qū)偏移(+08:00) | +08:00 |
5?? 文化相關(guān)格式化
DateTime now = DateTime.Now;
// 當(dāng)前文化
string currentCultureDate = now.ToString("D", CultureInfo.CurrentCulture);
// 特定文化 (例如英文)
string usCultureDate = now.ToString("D", new CultureInfo("en-US")); // Wednesday, November 15, 2023
// 中文文化
string zhCultureDate = now.ToString("D", new CultureInfo("zh-CN")); // 2023年11月15日6?? 實(shí)用擴(kuò)展方法
public static class DateTimeExtensions
{
public static string ToStandardString(this DateTime dt)
{
return dt.ToString("yyyy-MM-dd HH:mm:ss");
}
public static string ToCompactString(this DateTime dt)
{
return dt.ToString("yyyyMMddHHmmss");
}
public static string ToChineseDateString(this DateTime dt)
{
return dt.ToString("yyyy年MM月dd日");
}
}
// 使用方法
DateTime now = DateTime.Now;
string standard = now.ToStandardString(); // 2023-11-15 14:30:45
string compact = now.ToCompactString(); // 20231115143045
string chinese = now.ToChineseDateString(); // 2023年11月15日7?? 性能優(yōu)化建議
對于高頻調(diào)用的格式化操作:
- 預(yù)定義
IFormatProvider對象重復(fù)使用 - 預(yù)定義格式字符串常量
- 考慮使用
StringBuilder進(jìn)行復(fù)雜格式化
// 性能優(yōu)化示例
private static readonly IFormatProvider formatProvider = CultureInfo.InvariantCulture;
public static string FormatDateTimeFast(DateTime dt)
{
return dt.ToString("yyyy-MM-dd HH:mm:ss", formatProvider);
}選擇哪種方式取決于具體需求:
- 簡單格式化:直接使用
ToString方法 - 復(fù)雜字符串:使用字符串插值或
String.Format - 國際化應(yīng)用:使用文化相關(guān)的格式化
- 需要復(fù)用代碼:使用擴(kuò)展方法
到此這篇關(guān)于C# 獲取指定格式時間字符串的幾種方法的文章就介紹到這了,更多相關(guān)C# 獲取指定格式時間字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C#和Spire.PDF庫實(shí)現(xiàn)PDF與OFD格式互轉(zhuǎn)的具體教程
在日常文檔處理工作中,經(jīng)常需要在不同格式之間進(jìn)行轉(zhuǎn)換,PDF作為一種通用文檔格式已被廣泛使用,而OFD則是我國自主制定的開放版式文檔標(biāo)準(zhǔn),本文將介紹如何使用C#和Spire.PDF庫實(shí)現(xiàn)這兩種格式之間的相互轉(zhuǎn)換,需要的朋友可以參考下2025-09-09
ListView用法中與滾動相關(guān)的需求實(shí)現(xiàn)
這篇文章主要介紹了ListView用法中與滾動相關(guān)的需求實(shí)現(xiàn),獲取并設(shè)置ListView的滾動位置,以及獲取滾動位置處的項(xiàng)目,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
C#中C/S端實(shí)現(xiàn)WebService服務(wù)
本文主要介紹了C#中C/S端實(shí)現(xiàn)WebService服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
在C# WinForm應(yīng)用中實(shí)現(xiàn)多語種切換功能
本文將介紹如何在一個WinForm應(yīng)用程序中實(shí)現(xiàn)多語種切換,通過一個簡單的示例,你將了解到如何使用資源文件管理不同語言的文本,并通過用戶界面實(shí)現(xiàn)語言切換,需要的朋友可以參考下2024-06-06
C#實(shí)現(xiàn)modbus通訊的實(shí)現(xiàn)示例
本文主要介紹了modbus通訊的串口(RTU)、網(wǎng)口(TCP)通訊的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
C# Web應(yīng)用調(diào)試開啟外部訪問步驟解析
本文主要介紹了C# Web應(yīng)用調(diào)試開啟外部訪問的實(shí)現(xiàn)過程與方法。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01

