MCPcopy Create free account
hub / github.com/XIU2/UserScript / parseTime

Function parseTime

Lanzou-Enhanced.user.js:786–835  ·  view source on GitHub ↗
(now, text)

Source from the content-addressed store, hash-verified

784 }
785 // 转换时间格式
786 function parseTime(now, text) {
787 function parseDay(time) {
788 const days = [
789 {
790 name: '前天',
791 offset: -2,
792 },
793 {
794 name: '昨天',
795 offset: -1,
796 }
797 ];
798 const date = new Date(now.getTime());
799 let index = -1;
800 for (let i = 0; i < days.length; i++) {
801 if (time.indexOf(days[i].name) > -1) {
802 index = i;
803 break;
804 }
805 }
806 if (index === -1) return new Date(now.getTime());
807 const hourAndMinute = text.replace(days[index].name, '').split(':');
808 const hour = parseInt(hourAndMinute[0]);
809 const minute = parseInt(hourAndMinute[1]);
810 date.setDate(date.getDate() + days[index].offset);
811 date.setHours(hour);
812 date.setMinutes(minute);
813 // 蓝奏云显示比较奇怪,超过24小时,未满48小时,都是昨天,并不以每天0点作为分界
814 if (hour * 60 + minute > now.getHours() * 60 + now.getMinutes()) {
815 date.setDate(date.getDate() - 1);
816 }
817 return date;
818 }
819 if (text.indexOf('秒前') > -1) {
820 return now.getTime() - parseInt(text.replace('秒前', '')) * 1000;
821 } else if (text.indexOf('分钟前') > -1) {
822 return now.getTime() - parseInt(text.replace('分钟前', '')) * 60 * 1000;
823 } else if (text.indexOf('小时前') > -1) {
824 return now.getTime() - parseInt(text.replace('小时前', '')) * 60 * 60 * 1000;
825 } else if (text.indexOf('昨天') > -1 || text.indexOf('前天') > -1) {
826 return parseDay(text).getTime();
827 } else if (text.indexOf('天前') > -1) {
828 return now.getTime() - parseInt(text.replace('天前', '')) * 24 * 60 * 60 * 1000;
829 } else if (text.indexOf('月前') > -1) { // 我不知道有没有以下几种情况,暂时先写上
830 return now.getTime() - parseInt(text.replace('月前', '')) * 30 * 24 * 60 * 60 * 1000;
831 } else if (text.indexOf('年前') > -1) {
832 return now.getTime() - parseInt(text.replace('年前', '')) * 365 * 24 * 60 * 60 * 1000;
833 }
834 return Date.parse(text);
835 }
836
837 // 排序
838 function sortItems(files, by, order) {

Callers 1

getFilesFunction · 0.85

Calls 1

parseDayFunction · 0.85

Tested by

no test coverage detected