2

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?

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. Commented Sep 26, 2011 at 3:35
  • phpjs.org/functions/strtotime Commented Sep 26, 2011 at 3:36
  • Unforunatly this data is coming from an XML source that is formating the date in this way :( Commented Sep 26, 2011 at 3:37

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

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.

1 Comment

actually it looked like it is always in front, and always separated by a space, so a split() will do
0
var reTYD = /(today|yesterday|\d{1,2}\/\d{1,2}\/\d{4})/i;
console.log( myString.match(reTYD) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.