Updated April 1, 2023
<script> // converting timestamp into milliseconds var t_stamp = xyz; var t_stamp_ms = t_stamp *1000; //creating the date object var obj = date(t_stamp_ms); var hours = getHours(obj); var min = getMinutes(obj); … … … … //printing values on the console </script>
Where xyz is a numerical UNIX timestamp value.
- getDate(): This method provides the date for the timestamp which the user gives. Its value lies between the range (1 – 31).
- getHours(): This method returns the actual hours for the timestamp provided by the user. Its value lies between 0-11.
- getMinutes(): This method returns the actual minutes for the timestamp provided by the user. Its range lies between 0- 59.
- getSeconds(): This method returns the seconds for the timestamp provided by the user. Its value lies between 0 – 59.
- getMonth(): This method provides the 2 digit month to the user for the timestamp provided, which lies between 0 – 11. The result needs to be incremented by 1 in order to match the actual calendar month.
- getFullYear(): As the name indicates, this method provides the full year in 4 digits for the timestamp provided to the user.
- The value of the timestamp returned after multiplying with 1000 is passed to the Date() in order to create the date object.
- Once the date object is created, almost everything is sorted. Now the individual values are extracted from the Date object, like the Minute value is extracted using getMinutes() method, seconds value using getSeconds(), month value is extracted using getMonths(), etc.
- In order to Extract the last 2 digits off Months, Date, Hours and seconds, the slice() function is used, and padding with the extra ‘0’ is done.
- The final date is printed on the console by concatenating all the individual date, time, seconds values according to the requirement of the programmer using the required separators (/, :, – ).
- Individual values are extracted using the Date() functions like getHours(), getMinutes(), getFullYear(), etc and padding is done with the “0”. The slice function is used to slice or extract the last 2 digits of the respective functions. Finally, in order to print the entire date in the respective format, concatenation is done using the ‘+’ operator and separators like ‘/’ and ‘: ‘ are used.