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

javascript - string.slice() not working as expected - Stack Overflow

programmeradmin1浏览0评论

I am using slice()to take apart strings, but it does not work correctly.

strings:

var datept = "2018-01-19"
var timept = "12:05"

disjoint:

var month = datept.slice(5, -3); // should: "01" is: "01"
var day = datept.slice(8, -0); // should: "19" is: ""    --  WRONG
var year = datept.slice(0, -6); // should: "2018" is: "2018"
var hours = timept.slice(0, -3); // should: "12" is: "12"
var minutes = timept.slice(3, -0); // should: "05" is: "" -- WRONG

also tried:

var day = datept.slice(-8, -0); // or
var day = datept.slice(8, -0);  // or
var day = datept.slice(-8, 0);  // or
var day = datept.slice(8, 0);

I am using slice()to take apart strings, but it does not work correctly.

strings:

var datept = "2018-01-19"
var timept = "12:05"

disjoint:

var month = datept.slice(5, -3); // should: "01" is: "01"
var day = datept.slice(8, -0); // should: "19" is: ""    --  WRONG
var year = datept.slice(0, -6); // should: "2018" is: "2018"
var hours = timept.slice(0, -3); // should: "12" is: "12"
var minutes = timept.slice(3, -0); // should: "05" is: "" -- WRONG

also tried:

var day = datept.slice(-8, -0); // or
var day = datept.slice(8, -0);  // or
var day = datept.slice(-8, 0);  // or
var day = datept.slice(8, 0);

Share Improve this question edited Jan 19, 2018 at 14:01 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Jan 19, 2018 at 13:55 GucciGucci 9311 gold badge8 silver badges27 bronze badges 9
  • 5 splice !== slice – Nina Scholz Commented Jan 19, 2018 at 13:57
  • 1 He just got the title wrong Nina. No need to downvote – salezica Commented Jan 19, 2018 at 13:58
  • yeah, just spotted that too. – SANM2009 Commented Jan 19, 2018 at 13:58
  • 1 why dont you convert that string to a Date object? or even a moment object. then with the helper functions you could take the date hour and whatever you wanna do. – Nicholas Commented Jan 19, 2018 at 13:58
  • @slezica, i havn't dv. – Nina Scholz Commented Jan 19, 2018 at 14:02
 |  Show 4 more ments

4 Answers 4

Reset to default 4

You could split the date and use a destructuring assignment for the values.

var datept = "2018-01-19",
    timept = "12:05",
    [year, month, day] = datept.split('-'),
    [hours, minutes] = timept.split(':');
    
console.log(year, month, day);
console.log(hours, minutes);

Don't use -0. I'm not even sure what that's supposed to mean :) -0 is just 0, and slice will be confused by endIndex being lower than startIndex.

Here:

var day = datept.slice(8, -0); // should: "19" is: ""    --  WRONG

Instead:

var day = datept.slice(8); // should: "19" is: "19"

Anyway, there's better ways of doing this. You could manipulate a Date object (though those are not exactly intuitive), or use split() to get the fragments instead:

> var parts = "2018-01-19".split("-")
> parts[0] // '2018'
> parts[1] // '01'
> parts[2] // '19'

Using modern javascript, you can even:

> const [ year, month, day ] = "2018-01-19".split("-")
> year  // '2018'
> month // '01'
> day   // '19'

Why not using string.split()?

let str = "2018-01-19";
let parts = str.split("-");
let day = parts[2];
let month = parts[1];
let year = parts[0];

This solution its simply stupid. Why won't you convert to a Date object (native) or even a use moment (you will have to import it) and then get what you want with the help of helper functions?

let c = new Date("2018-01-19")
c.getDate() // 19
c.getMonth() + 1 // 0 (its from 0-11)

and soo on. See more documentation for each on highlights.

发布评论

评论列表(0)

  1. 暂无评论