Search code examples
javascripttimefetch

get time format to hh:mm


I fetch a specific time from an JSON. The value I get for the start-time looks like this: 2021-02-15T20:30:00+01:00 But I need to get it in hh:mm. I tried Date.parse() but I am unable to get the hours and minutes from this value. How can I do this else?

fetch('XXX')
        .then(Response => Response.json())
        .then(data => { 

    var summary = data['items']['0']['summary'];
    var location = data['items']['0']['location'];
    var start = data['items']['0']['start']['dateTime'];
    var end = data['items']['0']['end']['dateTime'];

Solution

  • I've always liked this method for simplicity.

    let time = new Date();
    console.log(time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second:'numeric', hour12: true }));  
    console.log(time.toLocaleString('en-US', { hour: 'numeric', hour12: true }));  
    console.log(time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric'}));  
    console.log(time.toLocaleString('en-US', {second: 'numeric'}));