Unix时间戳与C# DateTime时间类型互换

/// <summary>
/// method for converting a UNIX timestamp to a regular
/// System.DateTime value (and also to the current local time)
/// </summary>
/// <param name="timestamp">value to be converted</param>
/// <returns>converted DateTime in string format</returns>
private static DateTime ConvertTimestamp(double timestamp)
{
    //create a new DateTime value based on the Unix Epoch
    DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);

    //add the timestamp to the value
    DateTime newDateTime = converted.AddSeconds(timestamp);

    //return the value in string format
    return newDateTime.ToLocalTime();
}

 

 

/// <summary>
/// method for converting a System.DateTime value to a UNIX Timestamp
/// </summary>
/// <param name="value">date to convert</param>
/// <returns></returns>
private double ConvertToTimestamp(DateTime value)
{
    //create Timespan by subtracting the value provided from
    //the Unix Epoch
    TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

    //return the total seconds (which is a UNIX timestamp)
    return (double)span.TotalSeconds;
}

转载于:https://www.cnblogs.com/dushouke/archive/2011/01/26/UNIX-Timestamp-DateTime.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值