How to format a date and time string in javascript

  • 2 days ago
In this video I'm showing a function which formats a date and time strings into hours if the time is for less than 24 hours and date for more than 24 hours.

Gist url https://gist.github.com/InterTuts/c515c060cb577885baf6119ddb0bbd68
Transcript
00:00In this video, I will show how to format a date and time stream using JavaScript.
00:13Here, if the post was created in the last 24 hours, I'm showing the hour.
00:24Otherwise, I'm showing the date.
00:31In the code, I'm using this function to format the date.
00:42First, I'm using the Date class to convert the date and time stream to a date object and get the milliseconds.
01:03I'm also using the Date class to obtain the current time and calculate the difference between the current time and the published time.
01:34Then, I'm turning the milliseconds into seconds.
01:43This is 24 hours in seconds.
01:53In this way, I'm verifying if the difference is less than 24 hours.
02:04I'm showing the hours or date.
02:10I'm using now the 24 hours format.
02:16For the 12 hours format, I need to do these changes.
02:23And it works as expected.

Recommended