2

I don't know much about regular expressions, but I got a string (url) and I'd like to extract the date from it:

var myurl = "https://example.com/display/~test/2010/07/06/Day+2.+Test+Page";

I'd like to extract 2010/07/06 from it, additionally I would like to have it formatted as 6th of July, 2010.

3 Answers 3

3

Regex not required. A combination of split() and slice() will do as well:

var myurl = "https://example.com/display/~test/2010/07/06/Day+2.+Test+Page";
var parts = myurl.split("/");  // ["https:", "", "example.com", "display", "~test", "2010", "07", "06", "Day+2.+Test+Page"]
var ymd   = myurl.slice(5,8);  // ["2010", "07", "06"]
var date  = new Date(ymd);     // Tue Jul 06 2010 00:00:00 GMT+0200 (W. Europe Daylight Time)

There are several comprehensive date formatting libraries, I suggest you take one of those and do not try to roll your own.

Sign up to request clarification or add additional context in comments.

4 Comments

Passing an array to the Date() constructor won't work across all browsers. In Internet Explorer, you'd have to use new Date(ymd[0], ymd[1] - 1, ymd[2]);.
@Andy: Thanks for the hint. As you might have guessed, I did not try it in IE.
yeah, I did guess that :-) I always remember that Internet Explorer 8 and lower's Date parsing is very poor. I think it's improved in 9, thanks to ECMAScript 5.
Thanks Tomalak, but the date conversion doesn't work since it only accepts dates without 0's prefixing the day or month. e.g 2010, 7, 6 gets accepted, but 2010, 07, 06 does not.
2

Depending on how the URL can change, you can use something like:

\/\d{4}\/\d{2}\/\d{2}\/

The above will extract /2010/07/06/ (the two slashes just to be safer - you can remove the heading and trailing \/ to get just 2010/07/06, but you might have issues if URL contains other parts that may match).

See the online regexp example here:

Here's the jsfiddle:

To format it, take a look e.g. here:

Something along these lines (note you need the function from above):

var dt = new Date(2010, 6, 6);
dateFormat(dt, "dS of mmmm, yyyy");
// 6th of June, 2010

3 Comments

Hi icyrock.com, thank you for your swift response! Can you be so nice to provide an example on how to implement this regexp? Thanks!
Thanks icyrock.com, but the date conversion doesn't work since it only accepts dates without 0's prefixing the day or month. e.g 2010, 7, 6 gets accepted, but 2010, 07, 06 does not.
@FLX Take a look at the full working example in the updated jsfiddle: jsfiddle.net/zwkDQ/1. I am using Firefox, so if it does not work for you and you are using IE, try parsing the strings before (i.e. instead of p[1] write parseInt(p[1]) and so on).
1
var myurl = "https://example.com/display/~test/2010/07/06/Day+2.+Test+Page";

var re = /(\d{4})\/(\d{2})\/(\d{2})/
var months = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

var parts = myurl.match(re)

var year = parseInt(parts[1]);
var month = parseInt(parts[2],10);
var day = parseInt(parts[3],10);

alert( months[month] + " " + day + ", " + year );

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.