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

有帮助吗?

解决方案

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(/ *\(.*\)/,""))))

其他提示

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"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top