增加了新的string.TryToDateTime方法。

This commit is contained in:
falcon 2023-01-04 16:13:05 +08:00
parent 785457a5f4
commit 819a5657e8

View File

@ -2,8 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Reflection.Metadata;
namespace Falcon.SugarApi namespace Falcon.SugarApi
{ {
@ -42,7 +40,6 @@ namespace Falcon.SugarApi
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> /// <summary>
/// 将字符串按格式转换为时间格式 /// 将字符串按格式转换为时间格式
/// </summary> /// </summary>
@ -69,12 +66,22 @@ namespace Falcon.SugarApi
try { try {
dt=str.ToDateTime(dateTimeFormat,culture); dt=str.ToDateTime(dateTimeFormat,culture);
return true; return true;
} catch(Exception) { }
catch(Exception) {
dt=default; dt=default;
return false; return false;
} }
} }
/// <summary>
/// 尝试将特定格式字符串转换为DateTime类型
/// </summary>
/// <param name="str">时间字符串</param>
/// <param name="dt">转换后的时间</param>
/// <param name="dateTimeFormat">时间格式</param>
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
/// <returns>成功True失败False</returns>
public static bool TryToDateTime(this string str,out DateTime dt,string dateTimeFormat = "",CultureInfo? culture = null)
=> str.TryToDateTime(dateTimeFormat,out dt);
/// <summary> /// <summary>
/// 返回字符串是否为指定格式的日期时间格式 /// 返回字符串是否为指定格式的日期时间格式
/// </summary> /// </summary>