简体   繁体   English

如何从包含日期范围的字符串中提取年份 [Typescript/Angular]

[英]How to extract years from a string that contains a date range [Typescript/Angular]

I'm a beginner in Typescript.我是打字稿的初学者。 I have a date range which I'm receiving as a string.我有一个以字符串形式接收的日期范围。

var range="7-01-2018 VS 5-01-2019";

It is hard coded right now just to explain you.现在硬编码只是为了向您解释。 But it will be in this string form only later also.但它也只会在稍后采用这种字符串形式。 I've to extract 2018 and store it in some variable, say startYear and also I've to extract 2019 and store it in endYear variable.我必须提取 2018 并将其存储在某个变量中,比如startYear ,我还必须提取 2019 并将其存储在endYear变量中。 So that later I can compare them and apply validations.以便稍后我可以比较它们并应用验证。

I tried using:我尝试使用:

  1. substring(start,end)子串(开始,结束)

My code worked but the problem is that the string will change its length when a double digit date is selected. My code worked but the problem is that the string will change its length when a double digit date is selected. For eg:例如:

  1. 7-01-2018 VS 25-01-2019 or 7-01-2018 VS 25-01-2019
  2. 17-01-2018 VS 5-01-2019 or 17-01-2018 VS 5-01-2019
  3. 27-01-2018 VS 25-01-2019

In these cases start and end will have different meanings.在这些情况下, startend将具有不同的含义。 What should I do now.我现在该怎么办。 Please help me.请帮我。

My method is:我的方法是:

myValidator() {

  range="7-01-2018 VS 5-01-2019";

  startYear=range.substring(5,8);

  endYear=range.substring(18,21);

  if(endYear<startYear) {
    console.log("Invalid range");
  }
}

Now I'm planning to use split() .现在我打算使用split() And split the string into different parts and then write some logic with that.并将字符串分成不同的部分,然后用它编写一些逻辑。 But the problem with split is that there's a VS in between.但是 split 的问题在于中间有一个VS However for separator parameter i can give - symbol.但是对于separator参数,我可以给出-符号。 Please correct me if I'm wrong.如果我错了,请纠正我。

  • Using String.prototype.match使用String.prototype.match
const range = '7-01-2018 VS 25-01-2019'


const [...range.matchAll(/\d{0,2}-\d{0,2}-(\d{0,4})/g)].map((date)=> date[1])

you are right split will be good你是对的分裂会很好

myValidator() {

      range="7-01-2018 VS 5-01-2019";

      startYear=parseInt(range.split("VS")[0].split("-")[2])

      endYear=parseInt(range.split("VS")[1].split("-")[2])

      if(endYear<startYear) {
        console.log("Invalid range");
      }
    }

全部在一条线上

const [,startYear,endYear]=range.match(/.*-(\d{4}) VS .*-(\d{4})$/);

Using split , Date and reduce :使用splitDatereduce

 console.log("7-01-2018 VS 25-01-2019: ", checkRange("7-01-2018 VS 25-01-2019")); console.log("7-01-2019 VS 25-01-2018: ", checkRange("7-01-2019 VS 25-01-2018")); function checkRange(inputRange) { const range = inputRange.split(" VS "); const getYear = dateStr => new Date(dateStr.split("-").reverse().join("-")).getFullYear(); const years = range.reduce( (acc, val) => [...acc, getYear(val)], []); return years[0] < years[1]; }
 .as-console-wrapper { top: 0; max-height: 100% !important; }

Try this one, edited for array of integers as Output试试这个,编辑整数数组作为输出

myValidator() {
  range = "7-01-2018 VS 5-01-2019";
  regEx = /\d{4}/g;
  return (range.match(regEx).map(x => parseInt(x,10));
}
Output :
[ 2018, 2019 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM