最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getDate() slice() confusion - Stack Overflow

programmeradmin2浏览0评论

Hello I'm trying to remove the first number of a date. Let's say today was 15 May, how would I

  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.

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

  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.

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?

Share Improve this question edited May 5, 2016 at 19:57 Nayuki 18.6k6 gold badges58 silver badges84 bronze badges asked May 5, 2016 at 19:23 Dimitris XydasDimitris Xydas 2332 gold badges5 silver badges9 bronze badges 6
  • 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 a slice 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
 |  Show 1 more ment

4 Answers 4

Reset to default 3

slice() 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;
发布评论

评论列表(0)

  1. 暂无评论