-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix humanizar fechas de varios tipos
- Loading branch information
Daniel Restrepo
committed
Jul 21, 2023
1 parent
edd1b73
commit 46347e4
Showing
2 changed files
with
28 additions
and
23 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
import dayjs from "dayjs"; | ||
import relativeTime from "dayjs/plugin/relativeTime.js"; | ||
import customParseFormat from "dayjs/plugin/customParseFormat" | ||
|
||
await import("dayjs/locale/en-au.js") | ||
|
||
dayjs.locale("en-au") | ||
dayjs.extend(relativeTime) | ||
dayjs.extend(customParseFormat) | ||
|
||
function toHumanize(date) { | ||
const wrappedDate = dayjs(date) | ||
const wrappedDate = dayjs(date, "DD-MM-YYYY") | ||
const daysDiff = Math.abs(wrappedDate.diff(Date.now(), "days")); | ||
|
||
const isCurrentYear = wrappedDate.year() === new Date().getFullYear(); | ||
const isCurrentYear = wrappedDate.year() === new Date().getFullYear(); | ||
|
||
if(!isCurrentYear) { | ||
return wrappedDate.format("MMMM DD, YYYY") | ||
} else if(daysDiff <= 30) { | ||
return wrappedDate.fromNow(); | ||
} else { | ||
return wrappedDate.format("MMMM DD") | ||
} | ||
if(!isCurrentYear) { | ||
return wrappedDate.format("MMMM DD, YYYY") | ||
} else if(daysDiff <= 30) { | ||
return wrappedDate.fromNow(); | ||
} else { | ||
return wrappedDate.format("MMMM DD") | ||
} | ||
} | ||
|
||
|
||
const toPrint = toHumanize(dayjs().subtract(365, "days")) | ||
const dates = document.querySelectorAll("time") | ||
dates.forEach(function(date) { | ||
|
||
const datetime = date.getAttribute("datetime") | ||
|
||
console.log("datetime: " + datetime) | ||
|
||
var cardDate = document.getElementsByClassName("card-date"); | ||
|
||
for (var i = 0; i < cardDate.length; i++) { | ||
cardDate[i].innerHTML = toPrint; | ||
} | ||
const transformDate = toHumanize(datetime); | ||
console.log(transformDate) | ||
date.textContent = transformDate | ||
}) |