Created
June 22, 2020 03:42
-
-
Save ulises-jeremias/c5ff7325320ac994ea173e89632874d0 to your computer and use it in GitHub Desktop.
Date utils using lodash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash'; | |
export const dateFormat = (date) => { | |
const dateStr = String(date || ''); | |
if (!dateStr) { | |
return dateStr; | |
} | |
return String(dateStr.slice(0, 10) || '') | |
.replace(/^(\d\d)(\d)$/g, '$1/$2') | |
.replace(/^(\d\d\/\d\d)(\d+)$/g, '$1/$2'); | |
}; | |
export const timeFormat = (time) => { | |
const timeStr = String(time || ''); | |
if (!timeStr) { | |
return timeStr; | |
} | |
return String(timeStr.slice(0, 10) || '') | |
.replace(/^(\d\d)(\d)$/g, '$1:$2') | |
.replace(/^(\d\d:\d\d)(\d+)$/g, '$1:$2'); | |
}; | |
export const dateTimeFormat = (date) => { | |
const dateStr = String(date || ''); | |
if (!dateStr.trim()) { | |
return dateStr.trim(); | |
} | |
const formatedDate = String(dateStr.slice(0, 10) || '') | |
.replace(/^(\d\d)(\d)$/g, '$1/$2') | |
.replace(/^(\d\d\/\d\d)(\d+)$/g, '$1/$2'); | |
const formatedTime = String(dateStr.slice(11, 19) || '') | |
.replace(/^(\d\d)(\d)$/g, '$1:$2') | |
.replace(/^(\d\d:\d\d)(\d+)$/g, '$1:$2'); | |
if (dateStr.charAt(dateStr.length) === ' ') { | |
return dateStr; | |
} | |
if (dateStr.length <= 10) { | |
return formatedDate; | |
} | |
if (dateStr.length <= 19) { | |
return `${formatedDate} ${formatedTime}`; | |
} | |
return `${formatedDate} ${formatedTime} ${dateStr.slice(20, 22)}`; | |
}; | |
const validDateCondition = (date) => !( | |
Array.from(date).filter((w) => w !== '/').find(_.isNaN) | |
|| Array.from(date).filter((w) => w === '/').length > 2 | |
|| String(date).split('/').pop().length > 4 | |
|| String(date).split('/').pop() === '0000' | |
); | |
const validTimeCondition = (date) => !( | |
Array.from(date.split(' ')[0]).filter((w) => w !== ':').find(_.isNaN) | |
|| Array.from(date.split(' ')[0]).filter((w) => w === ':').length > 2 | |
|| _.isUndefined(date.split(' ')[1]) | |
); | |
const validDateTimeCondition = (date) => !( | |
!validDateCondition(date.slice(0, 10)) | |
|| !validTimeCondition(date.slice(11)) | |
); | |
const validDateConditions = { | |
date: validDateCondition, | |
time: validTimeCondition, | |
dateTime: validDateTimeCondition, | |
}; | |
export default { | |
dateFormat, | |
timeFormat, | |
dateTimeFormat, | |
validDateCondition, | |
validTimeCondition, | |
validDateTimeCondition, | |
validDateConditions, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment