Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Js return the start date and end date on a string

I have strings like this:

(Feb 28-Mar 1)
(Mar 2-3)

I would like me to return an object as you can see below, someone to give some suggestion how can I do?

    function rD(text){
    let date = text.replace('(', '').replace(')', '').split(' ');
    //const [start, end] = date[2].split("-").map(Number);
    return date;
    }
    
    
    console.log(rD("(Feb 28-Mar 1)"))
    console.log(rD("(Mar 2-3)"))

Return:

[
{
  month: 2,
  day: 28
},
{
  month: 3,
  day: 1
}
]

[
{
  month: 3,
  day: 2
},
{
  month: 3,
  day: 3
}
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'd remove the parentheses first and then split by /[ -]/. This way you get an array in one of these two forms

["Feb", "28", "Mar", "1"]

or

["Mar", "2", "3"]

Now if the array has 4 elements the first and third are always the month and second and forth are the day. If the array has 3 elements, the first is a month for both, start and end, the second and third are the days.

For getting the number of the month you can have a simple lookup like

{ Jan:1, Feb:2, ... }

let months = { Jan: 1, Feb: 2, Mar: 3 /*  you get the idea*/}
let spans = ["(Jan 28 - Feb 3)", "(Mar 1-3)"]

let parse = (span) => {
  let parts = span.replace(/[()]/g, "").split(/[ -]/).filter(x => !!x);
  switch (parts.length) {
    case 4: return [{month: months[parts[0]], date: +parts[1]}, {month: months[parts[2]], date: +parts[3]}];
    case 3: return [{month: months[parts[0]], date: +parts[1]}, {month: months[parts[0]], date: +parts[2]}];
    default: return undefined;
  }
}


console.log(parse(spans[0]));
console.log(parse(spans[1]))

about 4 years ago · Juan Pablo Isaza Report

0

I'd suggest using a regex pattern to parse each span.

From this we can get the startMonth, startDay, endMonth, endDay. We can then create a getMonthNumber() function to turn the abbreviated month name (Jan, Feb, etc.) to a number.

function getMonthNumber(month) {
    const lookup = { jan: 01, feb: 02, mar: 03, apr: 04, may: 05, jun: 06, jul: 07, aug: 08, sep: 09, oct: 10, nov: 11, dec: 12};
    return lookup[(month + '').toLowerCase()]
}

function parseSpan(str) {
    const pattern = /\(([a-z]{3})\s+(\d{1,2})\-([a-z]{3})?\s?(\d{1,2})\)/i
    const [, startMonth, startDay, endMonth, endDay] = str.match(pattern);
    return [
        { month: getMonthNumber(startMonth), day: +startDay },
        { month: getMonthNumber(endMonth || startMonth), day: +endDay }
    ];
}

let testInputs = [
  '(Feb 28-Mar 1)',
  '(Mar 2-3)',
  '(Sep 28-Oct 31)',
  '(Jan 3-17)'
]

testInputs.map(parseSpan).forEach(span => console.log(span))
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Report

0

You can try this

function rangeCalcFunc(range = null) {
  if(range && range.length){
    const [start, end] = range.substring(1, range.length-1).split("-");
    console.log(start);console.log(end);
    const [startMon, startDt] = start.split(" ");
    const [endMon, endDt] = end.split(" ");
    
    return [
    {
      month: calcMonthInNumber(startMon.trim()),
      date: startDt
    },
    {
      month: calcMonthInNumber(endMon.trim()),
      date: endDt
    }
    ]
  }
}

function calcMonthInNumber(month) {
    switch(month.toLowerCase()){
      case 'jan': return '01'
      case 'feb': return '02'
      //add for other months
      default: break;
    }
}

console.log(rangeCalcFunc("(Jan 28-Feb 1)"));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!