简体   繁体   中英

Javascript days output to range format?

I have a javascript object which contains the days of the week and a true/false value if they have been selected or not.

I am then passing this object to a function where I need to format the days in a specific way and return it.

Essentially, if any days follow another day that is also true, it will display those selected days as a range such as MF . Where as if the days selected were M, T, F then the output would be MT, F

Here is an example of how I am trying to set this up:

// Define our selected days
var days = {
  sunday: true,
  monday: false,
  tuesday: false,
  wednesday: true,
  thursday: true,
  friday: false,
  saturday: false
};

// Format our dates
function formatDates(days) {
  // Logic Here
  return days;
}

console.log(formatDates(days));

// Scenarios 

/* Input 
1: Checked: Su, W, Th
2: Checked: Su, M, T, Sa
3: Checked: M, T, Th, Sa
4: Checked: T, Th, Sa, Sun
/*

/*
Output
1: Su, W, Th
2: Sa - T
3: M - T, Th, Sa
4: Sa - Su, T, Th
/*

Here is a fiddle to the above code: https://jsfiddle.net/ghs3Lthj/1

Question: Are there any date functions that I should be aware of that handle ranges like this or would it need to be custom?

Just not sure if anything exists where I can say "shows me these days in a range-style format".

As properties in objects have no guaranteed order, you would better use an array for your days. Arrays are suitable when order is important.

Also, you would need to define the abbreviations for each day.

So I would suggest this structure:

var days = [
  { name: "sunday",    abbrev: "Su", selected: true  },
  { name: "monday",    abbrev: "M",  selected: false },
  { name: "tuesday",   abbrev: "Tu", selected: false },
  { name: "wednesday", abbrev: "W",  selected: true  },
  { name: "thursday",  abbrev: "Th", selected: true  },
  { name: "friday",    abbrev: "F",  selected: false },
  { name: "saturday",  abbrev: "Sa", selected: false }
];

I would then create a simple comma-separated string of all selected days, and then use string replacements to get to the desired format:

 function formatDates(days) { return days.map( day => day.selected ? day.abbrev : "" ) .join(", ") .replace(/(\\w+)(?:, \\w+)*, (\\w+)/g, "$1 - $2") // insert hyphen were appropriate .replace(/^(, )+|, (?=,|$)/g, "") // remove commas we don't need } var days = [ { name: "sunday", abbrev: "Su", selected: true }, { name: "monday", abbrev: "M", selected: false }, { name: "tuesday", abbrev: "Tu", selected: false }, { name: "wednesday", abbrev: "W", selected: true }, { name: "thursday", abbrev: "Th", selected: true }, { name: "friday", abbrev: "F", selected: false }, { name: "saturday", abbrev: "Sa", selected: false } ]; console.log(formatDates(days)); 

NB: Pairs of consecutive days will also be displayed with a hyphen, even though a comma would do. If you prefer the comma in that case, replace the * in the first regular expression with a + .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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