admin管理员组

文章数量:1401591

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 date portion of my string, ie. 'Today', 'Yesterday' or a date formatted as DD/MM/YYYY.

Is something like this possible at all with Javascript?

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 date portion of my string, ie. 'Today', 'Yesterday' or a date formatted as DD/MM/YYYY.

Is something like this possible at all with Javascript?

Share Improve this question edited Nov 23, 2011 at 23:30 Jonathan Leffler 756k145 gold badges951 silver badges1.3k bronze badges asked Sep 26, 2011 at 3:32 MarkMark 8532 gold badges10 silver badges11 bronze badges 3
  • If JavaScript was used to convert the date to 'Today' or 'Yesterday' in the first place, that JavaScript can be removed to get what you want. – bcm Commented Sep 26, 2011 at 3:35
  • phpjs/functions/strtotime – Joseph Silber Commented Sep 26, 2011 at 3:36
  • Unforunatly this data is ing from an XML source that is formating the date in this way :( – Mark Commented Sep 26, 2011 at 3:37
Add a ment  | 

5 Answers 5

Reset to default 3

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./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./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 e immediately after the day (like your example above. Here is a link with the proper syntax for JS regex. http://www.javascriptkit./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) );

本文标签: regexJavascript Extract a date from a stringStack Overflow