2016-08-15 48 views
-1

我想将此字符串分成4个变量。 “Sun Aug 28 2016 00:00:00 GMT + 0600(Central Asia Standard Time)” 我想变得像这样,var day =“Sun”var month =“Aug”var year = 2016; 我该如何在纯js中做到这一点?如何使用JavaScript将字符串分成4个变量

+1

您可能要到['Date'对象]上读了(https://developer.mozilla.org/EN-US /文档/网络/的JavaScript /参考/ Global_Objects /日)。 –

+0

尝试过“var res = str.split(”“);' –

回答

1

此外,您还可以,如果你的代码在支持该功能ES2015或正在transpiled到ES5的环境中运行使用解构。

var str = "Sun Aug 28 2016 00:00:00 GMT+0600 (Central Asia Standard Time)"; 
var [day, month, , year] = str.split(' '); 

console.log('day', day); 
console.log('month', month); 
console.log('year', year); 

Destructuring

+1

谢谢..请你解释一下('day',day ); –

+1

这就是我如何标记'console.log'语句,我只是觉得它更容易跟踪你在看什么,这是我个人的偏好。使用一个'console.log'来说明这个概念,就像Arnauld的回应一样。 –

2

您可以使用spli()

var my_string = "Sun Aug 28 2016 00:00:00 GMT+0600 (Central Asia Standard Time)"; 
var my_array = my:string.split(' '); 

console.log(my_array[0]); 
.... 
3

如果你问如何将一个日期字符串分割成组件块,你可以使用日期对象:

var datestr = "Sun Aug 28 2016 00:00:00 GMT+0600"; 
var date = new Date(Date.parse(datestr)); 
var year = date.getFullYear(); 

看这里以获取日期对象可用的函数列表: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear

但是,如果您希望对字符串进行更一般的拆分,则需要使用split,如前所述上面,或正则表达式专门绘制出不同的组件。事情是这样的:

/(\w+) (\w+) (\d{1,2}) (\d{4})/.exec(datestr); 

当然,上述的问题是,它假定一个非常具体的格式,这可能会或可能不会在你的情况可靠。

0

使用该日期字符串生成日期对象,然后通过属性访问所有值。

var d = new Date(dateString); 
    d.getDate() Returns the day of the month (from 1-31) 
    d.getDay() Returns the day of the week (from 0-6) 
    d.getFullYear() Returns the year 
    d.getHours() Returns the hour (from 0-23) 
    d.getMonth() Returns the month (from 0-11) 
0

首先,从您的日期字符串创建日期对象:

var date = new Date(yourDateString); 

然后你可以使用getDay()getMonth()getFullYear()功能。这些将返回数字,即0-6一天,0-11一个月和一年的一年。

为了让说,一个月为一个字符串,而不是一个数字,你可以建立一个数组:

month[0] = "January"; 
month[1] = "February"; 
month[2] = "March"; 
month[3] = "April"; 
month[4] = "May"; 
month[5] = "June"; 
month[6] = "July"; 
month[7] = "August"; 
month[8] = "September"; 
month[9] = "October"; 
month[10] = "November"; 
month[11] = "December"; 

var month = month[date.getMonth()]; 

要获得一天的字符串:

weekday[0]= "Sunday"; 
weekday[1] = "Monday"; 
weekday[2] = "Tuesday"; 
weekday[3] = "Wednesday"; 
weekday[4] = "Thursday"; 
weekday[5] = "Friday"; 
weekday[6] = "Saturday"; 

var day = weekday[date.getDay()]; 

要一年:

var year = date.getFullYear(); 
+0

仅供参考,getYear已弃用。请改用getFullYear。 –

+0

问题是由jQurey日历插件生成的字符串。 –

1

使用ES6 destructuring assignment,你可以这样做:

[ day, month,, year ] = str.split(' '); 

例子:

var str = "Sun Aug 28 2016 00:00:00 GMT+0600 (Central Asia Standard Time)", 
 
    day, month, year; 
 

 
[ day, month,, year ] = str.split(' '); 
 

 
console.log('day = ' + day + ', month = ' + month + ', year = ' + year);

+0

谢谢你这么好的例子!! –

1

这取决于你以后

  1. 你可以看着Date Object

    var date = new Date('Sun Aug 28 2016 00:00:00 GMT+0600 (Central Asia Standard Time)'); 
     
    console.log('Day of the week:', date.getDay()); 
     
    console.log('Full year:', date.getFullYear()); 
     
    console.log('Month:', date.getMonth());

    后,你可以,如果转换的数值与价值观做什么需要

  2. 或者干脆解析字符串日期使用正则表达式提取字符串值

相关问题