2016-08-15 128 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. 或者乾脆解析字符串日期使用正則表達式提取字符串值

相關問題