0

Database holds timestamps in this format - 03:59:59. Also I use third party plugin (autocomplete box) and it returns Date objects. I managed to use data from database in my plugin like below:

if (value) {
    let splitTime = value.split(':')
    date = new Date()
    // If ...splitTime (spread operator) is used in below, compiler throws exeption - "Expected 1 or 4 arguments but got 0 or more" (I consider it as TS bug)
    date.setHours(splitTime[0], splitTime[1], splitTime[2]);
    // now I can use date string in my plugin
}

Are there any simple ways to transform Date object to 00:00:00 format? As I start it appears to look ugly:

'' + args.value.getHours() + ':' + args.value.getMinutes() + ':' + args.value.getSeconds())

Result is still not what is needed:

//=> 3:59:59

(notice - 3 is not 03)

Julius Dzidzevičius
  • 10,775
  • 11
  • 36
  • 81

1 Answers1

2

Here is an example of what I commented:

function formatDate(date) {

    return (('0' + date.getHours()).slice(-2) +
        ':' +
        ('0' + date.getMinutes()).slice(-2) +
        ':' +
        ('0' + date.getSeconds()).slice(-2));
}

console.log(formatDate(new Date(2018, 6, 21, 1, 1, 1)));
Terry Wei
  • 1,521
  • 8
  • 16