2018-06-29 00:55:06 +00:00
|
|
|
/**
|
|
|
|
* @param {Date} [date] an optional date to convert to RFC2822 format
|
|
|
|
* @param {boolean} [useUtc=false] whether to parse the date as UTC (default: false)
|
|
|
|
* @returns {string} the converted date
|
|
|
|
*/
|
2019-09-18 18:36:52 +00:00
|
|
|
export function getRFC2822Date(date = new Date(), useUtc = false) {
|
2018-06-24 01:33:53 +00:00
|
|
|
if (useUtc) {
|
|
|
|
return getRFC2822DateUTC(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
const dates = date
|
|
|
|
.toString()
|
|
|
|
.replace('GMT', '')
|
|
|
|
.replace(/\s\(.*\)$/, '')
|
|
|
|
.split(' ');
|
|
|
|
|
|
|
|
dates[0] = dates[0] + ',';
|
|
|
|
|
|
|
|
const day = dates[1];
|
|
|
|
dates[1] = dates[2];
|
|
|
|
dates[2] = day;
|
|
|
|
|
|
|
|
return dates.join(' ');
|
|
|
|
}
|
2018-07-06 17:48:26 +00:00
|
|
|
|
2018-06-29 00:55:06 +00:00
|
|
|
/**
|
|
|
|
* @param {Date} [date] an optional date to convert to RFC2822 format (UTC)
|
|
|
|
* @returns {string} the converted date
|
|
|
|
*/
|
2019-09-18 18:36:52 +00:00
|
|
|
export function getRFC2822DateUTC(date = new Date()) {
|
2018-06-24 01:33:53 +00:00
|
|
|
const dates = date.toUTCString().split(' ');
|
|
|
|
dates.pop(); // remove timezone
|
|
|
|
dates.push('+0000');
|
|
|
|
return dates.join(' ');
|
|
|
|
}
|