+ 1

Error in current time detecting code

When I run the following code, the current date is displayed as 18/0/118 in stead of 18/01/2018. Anyone please help me detect the error- https://code.sololearn.com/Wd58kyjUfnAc/#js

17th Jan 2018, 7:16 PM
something
3 Respostas
+ 13
Use "getFullYear()" instead of: "getYear()". The month can be displayed in the format you want with some tricks: https://stackoverflow.com/questions/6040515/how-do-i-get-month-and-date-of-javascript-in-2-digit-format The code looks like: var curMonth = ("0" + (curTime.getMonth() + 1)).slice(-2); var curYear = curTime.getFullYear();
17th Jan 2018, 7:34 PM
Maz
Maz - avatar
+ 11
Hi @sagnik. The slice() method is used to extract a portion of a string/array, it accepts 2 params... the initial point in where you can extract the string (starts from "0") and the end point, if an end point is not specified, it extracts any letter from the beginning to the end of the string. If you use a negative value like in this case, it extracts the letters starting from the end of the string. In this case it extracts the last 2 chars from the string, i guess that's because the code is adding "0" to any month and you want to get something like: 01, 02, 03... but not: 010, 011... ... so, just take the last two characters from the string to remove the "0" if not needed.
18th Jan 2018, 8:52 AM
Maz
Maz - avatar
+ 2
Thank you very much. It helped, but can you please tell me a bit about the function of 'slice'. I guess I am hearing the term for the first time.
18th Jan 2018, 5:11 AM
something