最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

分配回对象时,日期格式不会更新(YYYYMMDD)

IT培训 admin 4浏览 0评论

分配回对象时,日期格式不会更新(YYYYMMDD)

我想要一个简单的YYYYMMDD格式,以便我可以在角度前端显示它。

在这里,对日期对象进行操作后,我想将它分配回结果对象中的TIMESTAMP列。 TIMESTAMP在数据库中设置为Date对象(postgres)。指定-时,TIMESTAMP列仍保留newDatex分隔符。

我甚至尝试过使用momentjs但无济于事。

我已经研究过this,但它无助于解决我的问题。我不希望在数据库中更新格式。我需要更新它,以便我可以发送更新格式(YYYYMMDD)到我的前端。

app.get('/api/:date', (req, res) => {
    var date = req.params.date;

    Model.findAll({
        where: {
            TIMESTAMP: {
                [Op.eq]: date
            }
        },
        order: [
            ['SYMBOL', 'ASC']
        ]
    }).then( result => {

        if (result != undefined){

            let i = 0;


            let year = new Date(result[i].TIMESTAMP)


            for (i = 0; i < result.length; i++) {
                if(result[i].TIMESTAMP != undefined){
                    let date = year.getDate().toString()
                    let month = (year.getMonth() + 1).toString()
                    let newyear = year.getFullYear().toString();
                    (date.length == 1) && ( date = '0' + date);
                    (month.length == 1) && (month = '0' + month);

                    let newDatex = new String();
                    newDatex = newyear + month + date;
                    console.log('modified date:', newDatex)
                    moment(result[i].TIMESTAMP).format("YYYYMMDD");

                    result[i].TIMESTAMP = new String();
                    result[i].TIMESTAMP = newDatex;

                    console.log('should be removing  "-" but isn\'t: ',result[i].TIMESTAMP)
                }
            }
            res.json(result)

        }
    })

});
回答如下:

我以任何方式将日期输入到新的Date()构造函数中,并按此格式化

var d = new Date('05 October 2011 14:48 UTC');
var day = d.getUTCDate();
var month = d.getUTCMonth() + 1;
var year = d.getFullYear();

// Check day for length to format
if (day.length < 2) {day = '0' + day};
console.log(`${year}${month}${day}`);

然后得到了这个:

也希望你没有在你引用的帖子中得到答案,那个人正在修改内置对象的原型链,这是一个巨大的禁忌。

如果这对您有用,请告诉我。

此外,如果您的数据类型是Postgres中的“Date”而不是varchar,它可能会在您存储时自动为您记录格式。因此,每次拉动数据时都可能需要更改数据类型或重新格式化数据。

分配回对象时,日期格式不会更新(YYYYMMDD)

我想要一个简单的YYYYMMDD格式,以便我可以在角度前端显示它。

在这里,对日期对象进行操作后,我想将它分配回结果对象中的TIMESTAMP列。 TIMESTAMP在数据库中设置为Date对象(postgres)。指定-时,TIMESTAMP列仍保留newDatex分隔符。

我甚至尝试过使用momentjs但无济于事。

我已经研究过this,但它无助于解决我的问题。我不希望在数据库中更新格式。我需要更新它,以便我可以发送更新格式(YYYYMMDD)到我的前端。

app.get('/api/:date', (req, res) => {
    var date = req.params.date;

    Model.findAll({
        where: {
            TIMESTAMP: {
                [Op.eq]: date
            }
        },
        order: [
            ['SYMBOL', 'ASC']
        ]
    }).then( result => {

        if (result != undefined){

            let i = 0;


            let year = new Date(result[i].TIMESTAMP)


            for (i = 0; i < result.length; i++) {
                if(result[i].TIMESTAMP != undefined){
                    let date = year.getDate().toString()
                    let month = (year.getMonth() + 1).toString()
                    let newyear = year.getFullYear().toString();
                    (date.length == 1) && ( date = '0' + date);
                    (month.length == 1) && (month = '0' + month);

                    let newDatex = new String();
                    newDatex = newyear + month + date;
                    console.log('modified date:', newDatex)
                    moment(result[i].TIMESTAMP).format("YYYYMMDD");

                    result[i].TIMESTAMP = new String();
                    result[i].TIMESTAMP = newDatex;

                    console.log('should be removing  "-" but isn\'t: ',result[i].TIMESTAMP)
                }
            }
            res.json(result)

        }
    })

});
回答如下:

我以任何方式将日期输入到新的Date()构造函数中,并按此格式化

var d = new Date('05 October 2011 14:48 UTC');
var day = d.getUTCDate();
var month = d.getUTCMonth() + 1;
var year = d.getFullYear();

// Check day for length to format
if (day.length < 2) {day = '0' + day};
console.log(`${year}${month}${day}`);

然后得到了这个:

也希望你没有在你引用的帖子中得到答案,那个人正在修改内置对象的原型链,这是一个巨大的禁忌。

如果这对您有用,请告诉我。

此外,如果您的数据类型是Postgres中的“Date”而不是varchar,它可能会在您存储时自动为您记录格式。因此,每次拉动数据时都可能需要更改数据类型或重新格式化数据。

发布评论

评论列表 (0)

  1. 暂无评论