Js 怎么把UTC时间到转换到当地时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Js 怎么把UTC时间到转换到当地时间相关的知识,希望对你有一定的参考价值。

参考技术A <script>
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
var date = d.getDate();
var hours = d.getHours();
var min = d.getMinutes();
var seconds = d.getSeconds();

var localDate = year+"-"+month+"-"+date + " " + hours + ":" + min + ":" + seconds;

console.log(localDate); //结果 2018-5-23 12:53:32

</script>

python utc和当地时间之间的日期时间转换


"""
Transform utc time system to local time
"""
def utc2local(dateutc):
    from datetime import datetime
    from dateutil import tz
    # setup
    from_zone = tz.tzutc()
    to_zone = tz.tzlocal()
    # transform
    try:
        # if dateutc is not a string is transformed previously
        utc = datetime.strptime(dateutc,"%Y-%m-%d %H:%M:%S")
    except:
        utc = dateutc
    utc = utc.replace(tzinfo=from_zone)
    datelocal = utc.astimezone(to_zone)
    # return datetime object
    return datetime(datelocal.year,datelocal.month,datelocal.day,datelocal.hour,datelocal.minute,datelocal.second)

        
"""
Transform utc time system to local time
"""
def local2utc(datelocal):
    from datetime import datetime
    from dateutil import tz
    # setup
    to_zone = tz.tzutc()
    from_zone = tz.tzlocal()
    # transform
    try:
        # if dateutc is not a string is transformed previously
        local = datetime.strptime(datelocal,"%Y-%m-%d %H:%M:%S")
    except:
        local = datelocal
    local = local.replace(tzinfo=from_zone)
    dateutc = local.astimezone(to_zone)
    # return datetime object
    return datetime(dateutc.year,dateutc.month,dateutc.day,dateutc.hour,dateutc.minute,dateutc.second)

以上是关于Js 怎么把UTC时间到转换到当地时间的主要内容,如果未能解决你的问题,请参考以下文章

js将UTC时间转化为当地时区时间

UTC 到夏令时的当地时间

将某人的当地时间转换为 UTC 时间

js utc转当地时间

python utc和当地时间之间的日期时间转换

在 C/C++ 中在当地时间和 GMT/UTC 之间转换