StrHelperToDateTimeExact 方法

将字符串转换为 DateTime(使用指定格式)

Definition

命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
public static DateTime ToDateTimeExact(
	string value,
	string format,
	DateTime defaultValue = default
)

参数

value  String
待转换的字符串;为 null 或空字符串时返回 defaultValue。
format  String
精确格式字符串(如 "yyyyMMdd"),按不变区域性解析。
defaultValue  DateTime  (Optional)
转换失败时返回的默认值。

返回值

DateTime
转换后的 DateTime;失败时返回 defaultValue。

备注

使用 TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime), 要求字符串与格式**完全匹配**(允许首尾空白已由调用方 Trim)。 适用于 "20240101"、"2024-01-01 12:00" 等确定格式的解析,避免 ToDateTime(String, DateTime) 的歧义。

参见