All files / components/Utils DateConversion.js

100% Statements 27/27
100% Branches 2/2
100% Functions 9/9
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 334x 864x 144x 144x 144x 144x 144x 144x 144x 144x     145x   149x   4x 147x 147x     4x 147x 147x       4x 70x 70x   70x  
export const toLocalISOString = (date) => {
    const pad = (num) => String(num).padStart(2, '0');
    const datePart = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
    const timePart = `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}`;
    const offset = calculateTimezoneOffset(date);
    const sign = calculateSign(offset);
    const hours = calculateHours(offset);
    const minutes = calculateMinutes(offset);
    const timezone = `${sign}${hours}:${minutes}`;
    return `${datePart}T${timePart}${timezone}`;
};
 
export const calculateTimezoneOffset = (date) => -date.getTimezoneOffset();
 
export const calculateSign = (offset) => (offset >= 0) ? '+' : '-';
 
export const calculateHours = (offset) => {
  const pad = (num) => String(num).padStart(2, '0');
  return pad(Math.floor(Math.abs(offset) / 60));
};
 
export const calculateMinutes = (offset) => {
  const pad = (num) => String(num).padStart(2, '0');
  return pad(Math.abs(offset) % 60);
};
 
 
export const DateConversion = (curr) => {
  const today = toLocalISOString(curr).split('T')[0];
  const nextMonth = toLocalISOString(new Date(curr.getFullYear(), today.slice(5, 7) % 12, today.slice(8, 10))).split('T')[0];
  
  return [today, nextMonth];
};