Dimitris Xydas Dimitris Xydas - 5 months ago 32x
Javascript Question

getDate() slice() confusion

Hello im trying to remove the first number of a date. Lets say today was 15 May, how would 1) remove the first integer and keep 5 only 2) be sure that that single digits (ex 5 June) stays the same? i tried the slice property but it doesn't seem to work.

var now = new Date();
var getTheDate = now.getDate().slice(-1);
console.log(getTheDate );

My fiddlesticks: https://jsfiddle.net/DimitriXd4/gp2eaaot

In theory slice(-1) should cut the last number and return it even if it a single number/word, right?


Try this:

var now = new Date();
var getTheDate = (now.getDate() + "").slice(-1);
console.log(getTheDate );


The empty string concatenated with the result, converts it to a string, then allowing slice to work