打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#DateTime与unix时间戳互相转换
public class UnixTimeUtil
{
    /// <summary>
    /// 将dateTime格式转换为Unix时间戳
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static int DateTimeToUnixTime(DateTime dateTime)
    {
        return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds;
    }
    /// <summary>
    /// 将Unix时间戳转换为dateTime格式
    /// </summary>
    /// <param name="time"></param>
    /// <returns></returns>
    public static DateTime UnixTimeToDateTime(int time)
    {
        if (time < 0)
            throw new ArgumentOutOfRangeException("time is out of range");
        return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddSeconds(time);
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 的时间戳转换
时间戳(Unix timestamp)转换工具
C#时间戳的简单实现
c#中的排序算法(动态数组ArrayList) - 欢迎来到jay的空间 - 博客园
收集的C#常用类代码
C#.ASP.NET公历转换农历代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服