From ff91c94af27c162938779d7029a0f5e2661cacc5 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Sat, 27 Aug 2022 15:42:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B0=86=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E8=BD=AC=E6=8D=A2=E4=B8=BADateTime=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/StringExtend.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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); + } } }