|
@@ -254,6 +254,80 @@ namespace ServiceCenter.Extensions
|
|
|
/// <returns></returns>
|
|
|
internal static T ConvertTo<T>(this object value) => (T)typeof(T).FromObject(value);
|
|
|
|
|
|
+ public static string yyyy(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyy);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string yyyyMM(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyyMM);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string yyyyMMdd(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyyMMdd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string yyyyMMddhh(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyyMMddhh);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string yyyyMMddhhmm(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyyMMddhhmm);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string yyyyMMddhhmmss(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyyMMddhhmmss);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static string yyyyMMddhhmmssf(this DateTime time)
|
|
|
+ {
|
|
|
+ return time.GetFormat(GetFormatterEnum.yyyyMMddhhmmssfffffff);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取指定格式时间的字符串
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="time">时间</param>
|
|
|
+ /// <param name="formatterEnum">类型</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string GetFormat(this DateTime time, GetFormatterEnum formatterEnum)
|
|
|
+ {
|
|
|
+ switch (formatterEnum)
|
|
|
+ {
|
|
|
+ case GetFormatterEnum.Default:
|
|
|
+ return time.ToString();
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyy:
|
|
|
+ return time.ToString("yyyy");
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyyMM:
|
|
|
+ return time.ToString("yyyy-MM");
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyyMMdd:
|
|
|
+ return time.ToString("yyyy-MM-dd");
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyyMMddhh:
|
|
|
+ return time.ToString("yyyy-MM-dd hh");
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyyMMddhhmm:
|
|
|
+ return time.ToString("yyyy-MM-dd hh:mm");
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyyMMddhhmmss:
|
|
|
+ return time.ToString("yyyy-MM-dd hh:mm:ss");
|
|
|
+
|
|
|
+ case GetFormatterEnum.yyyyMMddhhmmssfffffff:
|
|
|
+ return time.ToString("yyyy-MM-dd hh:mm:ss:fffffff");
|
|
|
+
|
|
|
+ default:
|
|
|
+ return time.ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static ConcurrentDictionary<Type, Func<string, object>> _dicFromObject = new ConcurrentDictionary<Type, Func<string, object>>();
|
|
|
|
|
|
public static object FromObject(this Type targetType, object value, Encoding encoding = null)
|
|
@@ -468,4 +542,47 @@ namespace ServiceCenter.Extensions
|
|
|
return sb.Append(">").ToString();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public enum GetFormatterEnum
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 默认类型
|
|
|
+ /// </summary>
|
|
|
+ Default = 0,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy
|
|
|
+ /// </summary>
|
|
|
+ yyyy = 4,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy-MM
|
|
|
+ /// </summary>
|
|
|
+ yyyyMM = 5,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy-MM-dd
|
|
|
+ /// </summary>
|
|
|
+ yyyyMMdd = 6,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy-MM-dd hh
|
|
|
+ /// </summary>
|
|
|
+ yyyyMMddhh = 7,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy-MM-dd hh:mm
|
|
|
+ /// </summary>
|
|
|
+ yyyyMMddhhmm = 8,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy-MM-dd hh:mm:ss
|
|
|
+ /// </summary>
|
|
|
+ yyyyMMddhhmmss = 9,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// yyyy-MM-dd hh:mm:ss:fffffff
|
|
|
+ /// </summary>
|
|
|
+ yyyyMMddhhmmssfffffff = 10,
|
|
|
+ }
|
|
|
}
|