Sorting, epoch convert, epoch range
Sat Nov 25 2023 06:42:26 GMT+0000 (Coordinated Universal Time)
Saved by
@StephenThevar
#javascript
//To sort string
Array.sort((a, b) => ("" + a).localeCompare(b, undefined, { numeric: true }));
//----------------------------------------------------
//To sort numbers
Array.sort((a,b) => a - b)
//----------------------------------------------------
//Convert epoch time to day - month - year
function convertEpochToDMY(epoch) {
let date = new Date(epoch * 1000);
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
day = day < 10 ? '0' + day : day;
month = month < 10 ? '0' + month : month;
return day + '-' + month + '-' + year;
}
//----------------------------------------------------
//this takes the start and end epoch val and gives out array of months
function monthArray(start, end) {
const arr = [];
let dt = new Date(start);
while (dt <= end) {
arr.push(dt.toLocaleString("en-US", { month: "short" }));
dt.setMonth(dt.getMonth() + 1);
}
return arr;
} // output [Dec, Jan]
//----------------------------------------------
const monthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
//function getMonthNames(startMonth, endMonth) {
// let result = [];
// for (let i = startMonth; i <= endMonth; i++) {
// result.push({ label: monthNames[i - 1], value: i });
// }
// return result;
// }
// this takes start and end month nos and gives list range of months
function getMonthNames(startMonth, endMonth) {
let result = [];
let month = startMonth;
while (true) {
result.push({ label: monthNames[month - 1], value: month });
if (month === endMonth) {
break;
}
month = (month % 12) + 1;
}
const sortedRes = result.sort((a, b) => (a.value - b.value) * -1);
return sortedRes;
}
//---------------------------------------------------
content_copyCOPY
Sorting using sort(). Epoch convert
Comments