1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- function friendlyDate(timestamp) {
- var formats = {
- 'year': '%n% 年前',
- 'month': '%n% 月前',
- 'day': '%n% 天前',
- 'hour': '%n% 小时前',
- 'minute': '%n% 分钟前',
- 'second': '%n% 秒前',
- };
- var now = Date.now();
- var seconds = Math.floor((now - timestamp) / 1000);
- var minutes = Math.floor(seconds / 60);
- var hours = Math.floor(minutes / 60);
- var days = Math.floor(hours / 24);
- var months = Math.floor(days / 30);
- var years = Math.floor(months / 12);
- var diffType = '';
- var diffValue = 0;
- if (years > 0) {
- diffType = 'year';
- diffValue = years;
- } else {
- if (months > 0) {
- diffType = 'month';
- diffValue = months;
- } else {
- if (days > 0) {
- diffType = 'day';
- diffValue = days;
- } else {
- if (hours > 0) {
- diffType = 'hour';
- diffValue = hours;
- } else {
- if (minutes > 0) {
- diffType = 'minute';
- diffValue = minutes;
- } else {
- diffType = 'second';
- diffValue = seconds === 0 ? (seconds = 1) : seconds;
- }
- }
- }
- }
- }
- return formats[diffType].replace('%n%', diffValue);
- }
- function getStorage(key) {
- //#ifdef H5
- const value = localStorage.getItem(key);
- return value !== null && value !== undefined ? value : undefined;
- //#endif
- //#ifndef H5
- const value = uni.getStorageSync(key);
- return value !== null && value !== undefined ? value : undefined;
- //#endif
- }
-
- function setStorage(key, value) {
- //#ifdef H5
- localStorage.setItem(key, value);
- //#endif
- //#ifndef H5
- return uni.setStorageSync(key, value);
- //#endif
- }
-
- function removeStorage(key) {
- //#ifdef H5
- localStorage.removeItem(key);
- //#endif
- //#ifndef H5
- return uni.removeStorageSync(key);
- //#endif
- }
-
- export {
- friendlyDate,
- getStorage,
- setStorage,
- removeStorage
- }
|