Time
Sometimes it can be a good idea to get the current time to use in script logic, reports etc.
JavaScript supports getting different units of time, for example, seconds, minutes and hours:
// Example Time: 25th June 2018 15:25:44
[Seconds] = new Date().getSeconds(); // Output: 44
[Minutes] = new Date().getMinutes(); // Output: 25
[Hours] = new Date().getHours(); // Output: 15
When collecting the time, the result that is returned will be dependent on the user's browser settings, more specifically, the system time settings for the user.
As these functions will return numbers, it will often be desired to take advantage of
Padding when dealing with times to ensure that it's always a two-digit number.
Date
Just like getting the time, the date is also a useful metric for logic and reports.
JavaScript also offers the date in various units like days, months and even years:
// Example Time: 25th June 2018 15:25:44
[Day] = new Date().getDate(); // Output: 25
[Month] = new Date().getMonth() + 1; // Output: 6
[Year] = new Date().getFullYear(); // Output: 2018
Due to the nature of JavaScript, months return a number between 0-11 and have to be increased by +1 to match normal month-numbering conventions. For example, if .getMonth() is used in March, the result would return 2 (which might be confused with February). A workaround for this is to add +1 to the returning value to bring March back to 3.
You can also use the .getDay() method, which will return the current day of the week as a number, starting from 0, which equates to Sunday.
Just like times, using
Padding may be preferable.
Article ID: 3260, Created: June 8, 2018 at 11:37 AM, Modified: July 6, 2018 at 1:21 PM