c# 时间帮助类
public class TimeHelper
{
/// <summary>
/// 获取时间戳 毫秒
/// </summary>
/// <returns></returns>
public static string GetTimeStamp(string format = "yyyyMMddHHmmssfff")
{
return DateTime.Now.ToString(format);
}
/// <summary>
/// 获取以0点0分0秒开始的日期
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DateTime GetStartDateTime(DateTime dt)
{
return Convert.ToDateTime(DateTime.Now.ToShortDateString());
}
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static DateTime JavascriptToDateTime(long timeStamp)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddMilliseconds(timeStamp);
return dt;
}
/// <summary>
/// 把时间转换为javascript所使用的时间
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static long DateTimeToJavascript(DateTime dt)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(dt - startTime).TotalMilliseconds; // 相差毫秒数
return timeStamp;
}
/// <summary>
/// 把时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public long DateTimeByUnix(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}
/// <summary>
/// 把Unix时间戳转化为时间
/// </summary>
/// <param name="unixTimeStamp"></param>
/// <returns></returns>
public DateTime DateTimeToUnix(long unixTimeStamp)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(unixTimeStamp);
return dt;
}
}