@livelike/react-native
Version:
LiveLike React Native package
35 lines (34 loc) • 1.51 kB
JavaScript
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const pad = n => {
return n < 10 ? '0' + n : n;
};
const timeConvert24hTo12h = value => {
let time = value.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)?$/) || [value];
if (time.length > 1) {
time = time.slice(1);
time[3] = +time[0] < 12 ? 'AM' : 'PM';
time[0] = +time[0] % 12 || 12;
}
return time.join('');
};
/**
* "JSC" compiler in Android doesn't not support ISO dateTime format in the form of "2022-05-26T07:32:50+0000"
* Although this format works in "hermes" js engine. Below Regex helps us to fix the unsupported date time format by
* converting it to "2022-05-26T07:32:50+00:00"
*/
const ANDROID_UNSUPPORTED_DATE_TIME_REGEX_FORMAT = new RegExp(/[\d\-:T]+\+\d{4}$/);
export const convertDateTime = date => {
let dateTimeObj = new Date(date);
if (!dateTimeObj.getDate()) {
if (ANDROID_UNSUPPORTED_DATE_TIME_REGEX_FORMAT.test(date)) {
dateTimeObj = new Date(`${date.slice(0, date.length - 2)}:${date.slice(-2)}`);
} else {
// In ideal case, this is Unreachable path but keeping convertDateTime util fn less strict
return '';
}
}
const formattedDate = `${dateTimeObj.getDate()} ${MONTHS[dateTimeObj.getMonth()]} ${dateTimeObj.getFullYear()}`;
const formattedTime = timeConvert24hTo12h(`${pad(dateTimeObj.getHours())}:${pad(dateTimeObj.getMinutes())}`);
return `${formattedDate} ${formattedTime}`;
};
//# sourceMappingURL=date.js.map