You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JwKdsV.Core.Utils
{
public class DateUtils
{
/// <summary>
/// 获取星期名称
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string GetWeekName(DateTime date)
{
string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
return Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString();
}
public static string GetNowTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
public static long GetTimeStamp(DateTime dateTime)
{
TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds);
}
/// <summary>
/// 毫秒时间戳格式化
/// </summary>
/// <param name="timeStamp"></param>
/// <param name="format"></param>
/// <returns></returns>
public static string GetFormatDateTime(string timeStamp, string format = null)
{
if (string.IsNullOrEmpty(timeStamp)) return null;
format = format ?? "yyyy-MM-dd HH:mm:ss";
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddMilliseconds(long.Parse(timeStamp));
return dt.ToString(format);
}
/// <summary>
/// 秒时间戳格式化
/// </summary>
/// <param name="secTimeStamp"></param>
/// <param name="format"></param>
/// <returns></returns>
public static string GetFormatDateTime4Sec(string secTimeStamp, string format = null)
{
if (string.IsNullOrEmpty(secTimeStamp)) return null;
format = format ?? "yyyy-MM-dd HH:mm:ss";
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(long.Parse(secTimeStamp));
return dt.ToString(format);
}
/// <summary>
/// 获取当前时间默认格式为yyyy-MM-dd HH:mm:ss
/// </summary>
/// <returns></returns>
public static string GetNowFormat()
{
return GetNowFormat("yyyy-MM-dd HH:mm:ss");
}
/// <summary>
/// 获取当前时间字符串,可以指定格式化
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
public static string GetNowFormat(string format)
{
format = format ?? "yyyy-MM-dd HH:mm:ss";
return DateTime.Now.ToString(format);
}
}
}