Hello I'm trying to remove the first number of a date. Let's say today was 15 May, how would I
- remove the first integer and keep 5 only
- be sure that that single digits (ex 5 June) stays the same? I tried the slice property but it doesn't seem to work.
My fiddlesticks:
var now = new Date();
var getTheDate = now.getDate().slice(-1);
console.log(getTheDate );
In theory slice(-1)
should cut the last number and return it even if it a single number/word, right?
Hello I'm trying to remove the first number of a date. Let's say today was 15 May, how would I
- remove the first integer and keep 5 only
- be sure that that single digits (ex 5 June) stays the same? I tried the slice property but it doesn't seem to work.
My fiddlesticks: https://jsfiddle/DimitriXd4/gp2eaaot
var now = new Date();
var getTheDate = now.getDate().slice(-1);
console.log(getTheDate );
In theory slice(-1)
should cut the last number and return it even if it a single number/word, right?
- now.getDate() returns 5 as it is – David Jones Commented May 5, 2016 at 19:28
- see this developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – David Jones Commented May 5, 2016 at 19:28
-
3
getDate
returns a number, which does not have aslice
method. You could either convert it to a string, or you could just use% 10
. – Bergi Commented May 5, 2016 at 19:31 - @DavidJones I mean yes today we have 5 may so it return 5, but if we had 16 may i would like to keep the second digit only(6). – Dimitris Xydas Commented May 5, 2016 at 19:32
-
Use
now.toDateString().slice(-1);
see updated fiddle: jsfiddle/gp2eaaot/2 – blurfus Commented May 5, 2016 at 19:33
4 Answers
Reset to default 3slice()
is String method, and getDate() doesn't return a String, thats why you can't call getDate().slice(-1);
You have to convert it to String, and then use slice.
e.g.
var getTheDate = now.getDate().toString().slice(-1);
But it doesn't change the original date variable, it will give you only sliced string. If you want to update the date you should do it manually.
There is no need to use slice()
. As Bergi suggested, it is easier to operate on the number by taking the remainder modulo 10.
var dateRightDigit = (new Date().getDate() % 10) + "";
console.log(dateRightDigit); // "0" or "1" or ... or "9"
This simpler approach of using modulo is preferable because when you slice a string, there are more concerns to consider. You have to know how long the string is, how the start
and end
arguments work, and what negative numbers mean.
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
The other answers do not seem to account for single digit numbers so here is a function that checks the date before slicing.
var sliceDate = function(dateObject){
var dateToSlice = dateObject.getDate();
//if the date is a two digit number
if (dateToSlice > 9) { //NOTE we should not have to worry about the date being negative
dateToSlice = dateToSlice.toString().slice(-1); //change to string then slice
}
return Number(slicedDate); //convert back to number and return
};
console.log(sliceDate(new Date())); //will print out 5 if May 5th or 6 if May 16th
Edit
Here is a more concise version:
var getTheDate = new Date().getDate();
var slicedDate = (getTheDate > 9) ? Number(getTheDate.toString().slice(-1)) : getTheDate;