时间戳与时区
所有文档
menu

函数计算 CFC

时间戳与时区

产品详情立即开通

CFC函数内的时间戳为Unix时间戳(Unix timestamp),时间为UTC世界标准时间。如果您需要在函数内打印格式化时间,需要指定时区,例如 "Asia/Shanghai"。以下提供nodejs和python两种语言的示例代码,仅供参考:

nodejs相关示例:

exports.handler = (event, context, callback) => {
    let d = new Date();
    console.log(d.toUTCString())
    console.log(d.toLocaleString("en-US", {timeZone: "Asia/Shanghai"}))
    console.log(d.toISOString())
    callback(null, "Hello world!");
};

python相关示例:

# -*- coding: utf-8 -*-
from datetime import datetime, timedelta, timezone

def handler(event, context): 
    utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
    print("utc datetime : ", utc_dt)
    bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
    print("bj datetime : ", bj_dt)
    return "Hello World"
上一篇
Baidu Serverless VSCode 插件
下一篇
函数运行环境及安全隔离性