mirror of
https://github.com/mue/mue.git
synced 2026-07-27 18:51:05 +02:00
Cleanup, refactor and new features
This commit is contained in:
63
src/components/widgets/time/Date.jsx
Normal file
63
src/components/widgets/time/Date.jsx
Normal file
@@ -0,0 +1,63 @@
|
||||
import React from 'react';
|
||||
import dtf from '@eartharoid/dtf';
|
||||
|
||||
export default class DateWidget extends React.PureComponent {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
this.state = {
|
||||
date: ''
|
||||
};
|
||||
}
|
||||
|
||||
getDate() {
|
||||
const date = new Date();
|
||||
const dateFormat = localStorage.getItem('dateFormat');
|
||||
|
||||
if (dateFormat === 'short') {
|
||||
const dateDay = date.getDate();
|
||||
const dateMonth = date.getMonth() + 1;
|
||||
const dateYear = date.getFullYear();
|
||||
|
||||
let day = dateDay, month = dateMonth, year = dateYear;
|
||||
|
||||
switch (dateFormat) {
|
||||
case 'MDY':
|
||||
day = dateMonth;
|
||||
month = dateDay;
|
||||
break;
|
||||
case 'YMD':
|
||||
day = dateYear;
|
||||
year = dateDay;
|
||||
break;
|
||||
default: break; // DMY
|
||||
}
|
||||
|
||||
let format;
|
||||
switch (localStorage.getItem('shortFormat')) {
|
||||
case 'dash': format = `${day}-${month}-${year}`; break;
|
||||
case 'gaps': format = `${day} - ${month} - ${year}`; break;
|
||||
default: format = `${day}/${month}/${year}`;
|
||||
}
|
||||
|
||||
this.setState({ date: format });
|
||||
} else { // full date
|
||||
const lang = localStorage.getItem('language');
|
||||
|
||||
const day = date.toLocaleDateString(lang, { weekday: 'long' });
|
||||
const nth = (localStorage.getItem('datenth') === 'true') ? dtf.nth(date.getDate()) : date.getDate();
|
||||
const month = date.toLocaleDateString(lang, { month: 'long' });
|
||||
const year = date.getFullYear();
|
||||
|
||||
this.setState({ date: `${day} ${nth} ${month} ${year}` });
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (localStorage.getItem('date') === 'false') return;
|
||||
this.getDate();
|
||||
}
|
||||
|
||||
render() {
|
||||
return <span style={{ 'textTransform': 'capitalize', 'fontWeight': 'bold' }}>{this.state.date}</span>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user