首页 > 编程 > .NET > 正文

UNIX时间戳与.net日期类的转换

2024-07-10 13:11:07
字体:
来源:转载
供稿:网友

1 将系统时间转换成unix时间戳
datetime dtstart = timezone.currenttimezone.tolocaltime(new datetime(1970,1,1));
datetime dtnow = datetime.parse(datetime.now.tostring());
timespan tonow = dtnow.subtract(dtstart);
string timestamp = tonow.ticks.tostring();
timestamp = timestamp.substring(0,timestamp.length - 7);2将unix时间戳转换成系统时间
string timestamp = "1144821796";
datetime dtstart = timezone.currenttimezone.tolocaltime(new datetime(1970,1,1));
long ltime = long.parse(timestamp + "0000000");
timespan tonow = new timespan(ltime);
datetime dtresult = dtstart.add(tonow);
短短几行代码,却花了我几个小时,就因为时区的问题,@[email protected]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表