[英]Splitting strings in JavaScript
I tried to split data as below, but the error "dat.split is not a function" displays. 我试图按如下所示拆分数据,但显示错误“ dat.split不是函数”。 Anyone know how can I solve this problem?
有人知道我该如何解决这个问题?
var dat = new Date("2009/12/12");
var r = dat.split('/');
You can't split()
a Date - you can split()
a String, though: 您不能
split()
日期-您可以split()
字符串,但是:
var dat = "2009/12/12";
var r = dat.split('/');
returns: 收益:
["2009", "12", "12"]
To do the equivalent with a date, use something like this: 要对日期进行等效处理,请使用类似以下的内容:
var dat = new Date();
var r = [dat.getFullYear(), dat.getMonth() + 1, dat.getDate()];
returns: 收益:
[2009, 4, 17]
try 尝试
dat.toString().split('/');
but this solution is locale dependent 但是此解决方案取决于语言环境
Do you just want to get the year, month and day? 您是否只想获取年,月和日? In that case you'd be better off using a non-locale dependent solution and calling the following functions:
在这种情况下,最好使用不依赖于语言环境的解决方案并调用以下函数:
dat.getDay();
dat.getMonth();
dat.getFullYear();
Sure they won't be zero padded, but that's easy enough to do. 当然,它们不会被零填充,但这很容易做到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.