So I was looking at how I could display a Desktop Notification using a Google Chrome extensions when I came across these lines of code:
var time = /(..)(:..)/(Date()); // The prettyprinted time.
var hour = time[1] % 12 || 12; // The prettyprinted hour.
var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // The period of the day.
What the heck does all of this do?
Fascinating, I’ve not seen this before:
EDIT: see this!
This:
Will return the match (array of matched groups) of the regular expression,
/(..)(:..)/, against the string (Date()):(or whatever time it happens to be)
The returned array (the match), in this case, is:
This line:
…simply determines the hour. If the hour is falsey (i.e.
0) then it defaults to12— this makes it possible for the next statement to return the correct am/pm suffix. (12:00 isam).