增加将字符串转换为DateTime扩展方法

This commit is contained in:
falcon 2022-08-27 15:42:47 +08:00
parent 9ca95224ae
commit ff91c94af2

View File

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