0

Can somebody please help me construct a regular expression in Javascript to check if year 2017 exists or a year starting with 19 or 20 (century) exists in a given date.

My date format is : Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time).

I tried this but it fails:

var str = "Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)";
var patt = new RegExp("/^\S{3}[\s]\S{3}[\s]\d{2}[\s]\d{4}$/");
var res = patt.test(str);

Thanks, Haseena

3
  • 2
    Use var patt = /^\S{3}[\s]\S{3}[\s]\d{2}[\s]\d{4}$/; or escape backslashes in string when using RegExp constructor. Commented Dec 29, 2016 at 5:34
  • 2
    Why not just use the Date('that long date string').getFullYear() Commented Dec 29, 2016 at 5:37
  • Possible duplicate of Get the Current Year in JavaScript Commented Dec 30, 2016 at 6:59

4 Answers 4

3

You can use Date.getFullYear() method.

var d = new Date("Fri Dec 01 1999 00:00:00 GMT-0800 (Pacific Standard Time)");
var year = d.getFullYear();
Sign up to request clarification or add additional context in comments.

2 Comments

It is easier in code to convert the string into a Date object to retrieve the year, but it's more work for the computer and completely unnecessary.
@Tamilselvan: i got year from above code and applied a regular expression on it ^(19|20)
0

Here is example you can use.

var str="Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)";
var patt=/(\w{3}\s){2}\d{2}\s(\d{4})\s(.*)/;
var results = patt.exec(str);
console.log(results);
console.log(results[2]);

The second group is match the year.

2 Comments

Too complex... Why do you explicitly match word characters, whitespace characters, and digits before or after the year? There are 11 characters that aren't relevant to the problem before the year, so you could just use . and it doesn't even matter what's after the year.
Yes, If only need the year it can simplify the Regular Expression. Thank you for advice.
0

The year validation with multiple condition is not quite a regex thingy

If I have understood your question then you can try this:

^[a-zA-Z]{3}\s+[A-Za-z]{3}\s+\d{2}\s+(2017|(?:19\d{2}|20\d{2}))\s+.*
  1. It will only match if the year is 2017
  2. It will match if the year starts with 19
  3. It will match if the year starts with 20
  4. If match is found, then the year can be found in group 1

Explanation

const regex = /^[a-zA-Z]{3}\s+[A-Za-z]{3}\s+\d{2}\s+(2017|(?:19\d{2}|20\d{2}))\s+.*$/gm;
const str = `Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)`;
let m;

 if((m = regex.exec(str)) !== null) {
   console.log("Full match ="+m[0]);
   console.log("Matched year ="+m[1]);
}
else
  console.log("no match found");

3 Comments

This is way too complex. Why use character ranges and whitespace metacharacters when only the year is required? Why not just extract the year with .match and test the value using String methods? Why specify a non-capturing group when a capturing group would allow you to retrieve the full year regardless of what it is? Why use the multiline flag?
Why would you just pick a 4 digit without knowing if the rest of the string really is in date format ? There can be 1000s of solution for the op's problem ... but as his desire is to fix it only via regex therefore my solution is to comply with his requirments
That's his criteria... he has a string representing a date in a fixed format that doesn't vary and he wants to make a decision based on the year in that string.
-1

With a fixed date format, this is really easy:

First, just extract your year using a regular expression var year = str.match(/^.{11}(\d{4})/)[1]

.match returns an array where the first element is the entire matched string and subsequent elements are the parts captured in parentheses.

After you have this, you can test if year === '2017' or if year.substr(0, 2) === '19'.

There are about a dozen other ways to do this, too.

var myDate = 'Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)';

function isCorrectYear(dateString) {
  var year = dateString.match(/^.{11}(\d{4})/)[1];
  if (year === '2017') return true;
}

if (isCorrectYear(myDate)) {
  alert("It's the correct year.");
}

Something just occurred to me... "year 2017 exists or a year starting with 19 or 20". Doesn't that cover every year in every date string you are ever likely to encounter? I don't think they had Javascript before the 1900s and I doubt anything any of us will write will still be in use after the year 2100.

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.