首页 > 开发 > 综合 > 正文

UNIX时间转换

2024-07-21 02:25:45
字体:
来源:转载
供稿:网友

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);

 

 

//datetime轉變為unix時間,直接转换,不改变时区
datetime dtstart = new datetime(1970, 1, 1, 0, 0, 0);
timespan tonow = dt.subtract(dtstart);
int timestamp = (int)(tonow.ticks/10000000);

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