简体   繁体   English

如何使用JavaScript将字符串分成4个变量

[英]How can I split a string into 4 variables using JavaScript

I wan to split this string into 4 variables . 我希望将此字符串分成4个变量。 "Sun Aug 28 2016 00:00:00 GMT+0600 (Central Asia Standard Time)" I want to get variable like this , var day="Sun" var month="Aug" var year=2016; “ 2016年8月28日星期日,格林尼治标准时间+0600(中亚标准时间)”我想获得这样的变量,var day =“ Sun” var month =“ Aug” var year = 2016; How can I do that in pure js? 如何在纯js中做到这一点?

If you're asking how to split a date string into component pieces, you can make use of the date object: 如果您要问如何将日期字符串分割成多个组成部分,则可以使用date对象:

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

See here for a list of functions available to the date object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear 请参阅此处以获取日期对象可用功能的列表: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear

However, if you want a more general split of the string, you would want to use split, as mentioned above, or regex specifically to draw out the different components. 但是,如果要对字符串进行更一般的拆分,则可以使用上述的split或正则表达式专门绘制不同的组件。 Something like this: 像这样:

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

Of course the problem with the above is that it presumes a very specific format, which may or may not be reliable in your situation. 当然,上述问题在于它假定一种非常特定的格式,这种格式在您所处的情况下可能会或可能会不可靠。

You can use spli() 您可以使用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]);
....

Using ES6 destructuring assignment , you can do: 使用ES6 解构分配 ,您可以执行以下操作:

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

Example: 例:

 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); 

Also, you can use destructuring if your code is running in an environment that supports that ES2015 feature or is being transpiled to ES5. 此外,如果您的代码在支持该ES2015功能的环境中运行或正被移植到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 解构

It depends what you do with the values after 这取决于您对之后的值的处理方式

  1. You could look into the Date Object 您可以查看Date对象

     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()); 

    after you can convert the numerical values if needed 在您可以根据需要转换数值之后

  2. Or simply parse the string date with a regular expression to extract the string values 或简单地使用正则表达式解析字符串日期以提取字符串值

Make a Date object using that date string , then access all value by the properties . 使用该日期字符串创建Date对象,然后通过属性访问所有值。

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)

Firstly, create a date object from your date string: 首先,根据您的日期字符串创建一个日期对象:

var date = new Date(yourDateString);

Then you can use the getDay() , getMonth() and getFullYear() functions. 然后,您可以使用getDay()getMonth()getFullYear()函数。 These will return numbers ie 0-6 for day, 0-11 for month and just the year for year. 这些将返回数字,即0-6表示一天, 0-11表示月份,而年份则表示年份。

To get say the month as a string rather than a number you can set up an array: 要以字符串而不是数字来表示月份,可以设置一个数组:

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()];

To get the day as a string: 要将日期作为字符串:

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()];

To the year: 到当年:

var year = date.getFullYear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM