gpt4 book ai didi

javascript - 在 JavaScript 中将 Python UTC 时间戳转换为本地时间

转载 作者:行者123 更新时间:2023-11-30 00:30:31 25 4
gpt4 key购买 nike

我正在尝试将使用 python 生成的 UTC 时间戳转换为本地连接客户端时间。但是,时间永远不会正确转换。

python

import time, datetime


utc_time = datetime.datetime.utcnow()
utc_time = int(time.mktime(utc_time.timetuple())) * 1000

JavaScript

function Time(time) {
var month = {}
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sept";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
day = {}
day[0] = "Sunday";
day[1] = "Monday";
day[2] = "Tuesday";
day[3] = "Wednesday";
day[4] = "Thursday";
day[5] = "Friday";
day[6] = "Saturday";
var b = new Date();
var d = new Date(time);
var c = d.getTime() + (b.getTimezoneOffset() * 60000);
var offset = -(b.getTimezoneOffset() /60);
var res = c + (3600000 * offset);
d.setTime(res);
var days = day[d.getDay()];
var mon = d.getMonth();
var year = d.getFullYear();
var hour = d.getHours();
var minute = d.getMinutes();
var seconds = d.getSeconds();
var date = d.getDate();
return days + ' - ' + month[mon] + '/' + date + '/' + year + " - " + hour + ":" + minute + ":" + seconds;
}
console.log(Time(1429490580*1000));
// time stamp generated: Sunday - Apr/19/2015 at 4:43PM

是我的数学错误还是我在 python 方面做错了什么?

最佳答案

您的时间戳不正确:

import datetime
utc_time = datetime.datetime.utcnow()
timestamp = (utc_time - datetime.datetime(1970, 1, 1)).total_seconds()

关于javascript - 在 JavaScript 中将 Python UTC 时间戳转换为本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com