简体   繁体   中英

How can I split a string into 4 variables using JavaScript

I wan to split this string into 4 variables . "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; How can I do that in pure js?

If you're asking how to split a date string into component pieces, you can make use of the date object:

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

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

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:

[ 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.

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

     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 .

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. These will return numbers ie 0-6 for day, 0-11 for month and just the year for year.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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