diff --git a/Falcon.SugarApi/StringExtend.cs b/Falcon.SugarApi/StringExtend.cs index ef3a83f..4da479d 100644 --- a/Falcon.SugarApi/StringExtend.cs +++ b/Falcon.SugarApi/StringExtend.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Globalization; namespace Falcon.SugarApi { @@ -37,5 +38,18 @@ namespace Falcon.SugarApi /// 字符串数组。当str为null时返回空数组 public static string[] SplitStr(this string? str, params char[] splitChars) => str.IsNullOrEmpty() ? Array.Empty() : str.Split(splitChars.Length == 0 ? new char[] { ',', ',', ';', ';', '.', '。' } : splitChars, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + + /// + /// 将字符串转换为DateTime + /// + /// 表示日期时间的字符串 + /// 日期时间格式 + /// 一个DateTime对象。如果失败返回null + public static DateTime ToDateTime([NotNull] this string str, string dateTimeformat = "") { + if (dateTimeformat.IsNullOrEmpty()) { + return DateTime.Parse(str); + } + return DateTime.ParseExact(str, dateTimeformat, CultureInfo.CurrentCulture); + } } }