开发者

Javascript: Extract a date from a string?

开发者 https://www.devze.com 2023-04-07 18:55 出处:网络
I have a string formatted as either Today 3:28AM Yesterday 3:28AM 08/22/2011 3:28AM What I need to do is somehow extract into a variable the d开发者_JS百科ate portion of my string, ie. \'Today\', \

I have a string formatted as either

Today 3:28AM
Yesterday 3:28AM
08/22/2011 3:28AM

What I need to do is somehow extract into a variable the d开发者_JS百科ate portion of my string, ie. 'Today', 'Yesterday' or a date formatted as DD/MM/YYYY.

Is something like this possible at all with Javascript?


Since the JavaScript date parser won't recognize your dates, you can write a parser that puts the date into a format that it will recognize. Here is a function that takes the date examples that you gave and formats them to get a valid date string:

function strToDate(dateStr) {
    var dayTimeSplit = dateStr.split(" ");  
    var day = dayTimeSplit[0];
    var time = dayTimeSplit[1];

    if (day == "Today") {
        day = new Date();
    } else if (day == "Yesterday") {
        day = new Date();
        day.setDate(day.getDate() - 1);
    } else {
        day = new Date(day);
    }

    var hourMinutes = time.substring(0, time.length -2);
    var amPM = time.substring(time.length -2, time.length);

    return new Date((day.getMonth() + 1) + "/" + day.getDate() + "/" + day.getFullYear() 
        + " " + hourMinutes  + " " + amPM);
}

Then you can call stroToDate to convert your date formats to a valid JavaScript Date:

console.log(strToDate("Today 3:28AM")); 
console.log(strToDate("Yesterday 3:28AM"));
console.log(strToDate("08/22/2011 3:28AM"));

Outputs:

Sun Sep 25 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
Sat Sep 24 2011 03:28:00 GMT-0700 (Pacific Daylight Time)
Mon Aug 22 2011 03:28:00 GMT-0700 (Pacific Daylight Time)


Obviously "Today" and "Yesterday" can never be transformed back to a real numeric date, for now it seems that what are you trying to do here is to save it as "Today" and "Yesterday", right?

It appears that the dd/mm/yyyy hh:mmxx you specified is always separated by a space.

so you can just split the string into two, and save the first part as your date.

the javascript function: http://www.w3schools.com/jsref/jsref_split.asp

As for how to transform from "Today" back to 26/09/2011 etc, you need to seek solution from the XML side.


Here is a similar question: Javascript equivalent of php's strtotime()?

Here is the linked article: http://w3schools.com/jS/js_obj_date.asp

And the suggested solution:

Basically, you can use the date constructor to parse a date

var d=new Date("October 13, 1975 11:13:00");


There are a couple of ways you could do this. I will offer 2 of them.

option1: If the day always at the beginning of the string you could capture the the first part by using a regular expression like /([a-z0-9]*)\s|([0-9]{1,})\/([0-9]{1,})\/([0-9]{1,})\s/ <- im not the best regex writer.

option2: You could also do a positive look ahead if the time come immediately after the day (like your example above. Here is a link with the proper syntax for JS regex. http://www.javascriptkit.com/javatutors/redev2.shtml you can scroll down to lookaheads and see an example that should get you suared away there.


var reTYD = /(today|yesterday|\d{1,2}\/\d{1,2}\/\d{4})/i;
console.log( myString.match(reTYD) );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号