Frage

I am using the YouTube API and it returns uploaded & updated times in this format: 2013-05-13T13:12:42.000Z (ISO 8601). How can I get the relative time using Javascript (or jQuery)?

Some sources that I've tried to combine to get this to work, however they all appear to format the date differently.

Javascript time to relative

Format ISO-8601 into a date object

War es hilfreich?

Lösung

Use this timeDifference(new Date(), new Date().setTime(Date.parse("2013-05-13T13:12:42.000Z")))

If you have a problem with the "(ISO 8601)" part, use this timeDifference(new Date(), new Date().setTime(Date.parse("2013-05-13T13:12:42.000Z (ISO 8601)".replace(/ *\(.*\)/,""))))

Andere Tipps

Click here to check the Demo

var MyDate = new Date(
                    Date.parse
                    (
                        "2013-05-13T13:12:42.000Z (ISO 8601)"
                        .replace(/ *\(.*\)/,"")
                    )
                );

var date_Str = MyDate.getMonth()      + 
                         1            + 
                       "/"            + 
               MyDate.getDate()       + 
                       "/"            +  
               MyDate.getFullYear();

Add more function

1. MyDate.getHours()
2. MyDate.getMinutes()
3. MyDate.getSeconds()

Result - "5/13/2013"

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top