Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法在Lambda中创建时区在UTC旁边的日期对象_Node.js_Date_Datetime_Lambda_Momentjs - Fatal编程技术网

Node.js 无法在Lambda中创建时区在UTC旁边的日期对象

Node.js 无法在Lambda中创建时区在UTC旁边的日期对象,node.js,date,datetime,lambda,momentjs,Node.js,Date,Datetime,Lambda,Momentjs,我在Node.js中编写了一个Lambda。正在传递一个美洲/纽约时区日期时间字符串: exports.handler = async (event) => { // Stub out the response object const response = { statusCode: 200, body: "" }; const thingAsString = event.body.date; //This is a da

我在Node.js中编写了一个Lambda。正在传递一个美洲/纽约时区日期时间字符串:

exports.handler = async (event) => {
    // Stub out the response object
    const response = {
        statusCode: 200,
        body: ""
    };

    const thingAsString = event.body.date; //This is a date/time expressed in local time
    const thingAsObject = new Date(thingAsString); //This is the date/time with a TZ of UTC.

    console.log("Before: %s", thingAsString );
    console.log("After: %s", thingAsObject.toISOString());

    return response;
};
问题是,当我试图将其转换为日期/时间对象时,它假定时区为UTC

有没有办法将其转换为时区不立即设置为UTC的日期对象

我尝试了一些有关
时刻
莫奈时区
的选项,但我遇到了同样的问题:

const original_date_string = "2019/10/15 14:21:14";
const original_date_object = new Date(original_date_string);
const original_date_object_est = moment.tz(original_date_object, 'America/New_York');
console.log("Original: %s", original_date_string);
console.log("UTC: %s", original_date_object_est.utc().toISOString());

在创建时指定日期格式和时区,如下所示

const mtz = require('moment-timezone');
const moment = require('moment');

exports.handler = async (event) => {
    // Stub out the response object
    const response = {
        statusCode: 200,
        body: ""
    };
    // 2019/10/15 14:21:14
    const thingAsString = event.body.date;
    const thingAsObject = moment.tz(thingAsString, "YYYY/MM/DD HH:mm:ss", 'America/New_York');

    console.log(thingAsObject);
    console.log(thingAsObject.utc());

    return response;
};

2019/10/15 14:21:14
us非标准日期字符串。还有其他奇怪的处理方法。我无法控制向我发送的日期。如果它是以已知格式发送的,那么您仍然可以通过在瞬间提供格式或使用普通日期对象进行自定义解析来正确解析它。同意@VLAZ。演示答案。