File size: 740 Bytes
6931883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const HttpError = require('./httpError');

function normalizeDate(dateValue, fieldName = 'date') {
  const rawValue = String(dateValue || '').trim();

  if (/^\d{4}-\d{2}-\d{2}$/.test(rawValue)) {
    return rawValue;
  }

  if (/^\d{2}\/\d{2}\/\d{4}$/.test(rawValue)) {
    const [day, month, year] = rawValue.split('/');
    return `${year}-${month}-${day}`;
  }

  throw new HttpError(
    400,
    `${fieldName} must use YYYY-MM-DD or DD/MM/YYYY format.`,
    'INVALID_DATE',
  );
}

function mysqlDateTimeFromNow(daysAhead) {
  const expiryDate = new Date(Date.now() + daysAhead * 24 * 60 * 60 * 1000);
  return expiryDate.toISOString().slice(0, 19).replace('T', ' ');
}

module.exports = {
  normalizeDate,
  mysqlDateTimeFromNow,
};