feat: Refactor formatted_date component and add format function
This commit is contained in:
parent
7cd8db36e2
commit
7032c552a8
|
@ -3,22 +3,9 @@
|
||||||
|
|
||||||
if (typeof date === 'string') date = new Date(date);
|
if (typeof date === 'string') date = new Date(date);
|
||||||
|
|
||||||
const pad = function (str: string): string {
|
import { format } from "$lib/scripts/formatted_date";
|
||||||
return ('0' + str).slice(-2);
|
|
||||||
};
|
|
||||||
|
|
||||||
const year = date.getFullYear().toString();
|
const formattedDate = format(date);
|
||||||
const month = pad((date.getMonth() + 1).toString());
|
|
||||||
const day = pad(date.getDate().toString());
|
|
||||||
const hour = pad(date.getHours().toString());
|
|
||||||
const min = pad(date.getMinutes().toString());
|
|
||||||
const sec = pad(date.getSeconds().toString());
|
|
||||||
const tz = -date.getTimezoneOffset();
|
|
||||||
const sign = tz >= 0 ? '+' : '-';
|
|
||||||
const tzHour = pad((tz / 60).toString());
|
|
||||||
const tzMin = pad((tz % 60).toString());
|
|
||||||
|
|
||||||
let formattedDate = `${year}-${month}-${day}T${hour}:${min}:${sec}${sign}${tzHour}:${tzMin}`;
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{formattedDate}
|
{formattedDate}
|
||||||
|
|
20
src/lib/scripts/formatted_date.ts
Normal file
20
src/lib/scripts/formatted_date.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
export const format = (
|
||||||
|
date: Date
|
||||||
|
) => {
|
||||||
|
const pad = function (str: string): string {
|
||||||
|
return ('0' + str).slice(-2);
|
||||||
|
};
|
||||||
|
|
||||||
|
const year = date.getFullYear().toString();
|
||||||
|
const month = pad((date.getMonth() + 1).toString());
|
||||||
|
const day = pad(date.getDate().toString());
|
||||||
|
const hour = pad(date.getHours().toString());
|
||||||
|
const min = pad(date.getMinutes().toString());
|
||||||
|
const sec = pad(date.getSeconds().toString());
|
||||||
|
const tz = -date.getTimezoneOffset();
|
||||||
|
const sign = tz >= 0 ? '+' : '-';
|
||||||
|
const tzHour = pad((tz / 60).toString());
|
||||||
|
const tzMin = pad((tz % 60).toString());
|
||||||
|
|
||||||
|
return `${year}-${month}-${day}T${hour}:${min}:${sec}${sign}${tzHour}:${tzMin}`;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user