Unable to format default MySQL datetime

前端 未结 5 1271
礼貌的吻别
礼貌的吻别 2020-12-10 01:01

My dates come out of the database looking like this: 2013-11-21 17:43:20

I\'m trying to user Angular\'s date filter to turn them into something prettier

5条回答
  •  抹茶落季
    2020-12-10 01:35

    I created a pipe for this as follows

    import { Pipe } from "@angular/core";
    

    @Pipe({ name: 'DateToIso' }) export class DateToIso {

    transform(value, args) {
    
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    
    var parts=value.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    
    var converted = new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
    
    let newValue = converted.toISOString();
    
    return newValue;
    
    }
    

    }

提交回复
热议问题