Date.uts 12.0 KB
Newer Older
Y
yurj26 已提交
1 2 3 4 5 6 7 8 9 10 11
import { describe, test, expect, Result } from './tests.uts'

export function testDate() : Result {
    return describe("Date", () => {
        test('now', () => {
            const start = Date.now()
            setTimeout(() => {
                const millis = Date.now() - start
                const secondsElapsed = Math.floor(millis / 1000)
                expect(secondsElapsed).toEqual(2);
            }, 2000)
12 13 14 15 16 17 18
        })
        test('new Date', () => {
            let date1 = new Date('1992-02-02');
            console.log("1111",date1.getTime())
            expect(date1.getTime()).toEqual(696988800000);
            let date2 = new Date('1992-2-02');
            console.log("2222",date2.getTime())
lizhongyi_'s avatar
lizhongyi_ 已提交
19
            // expect(date2.getTime()).toEqual(696960000000);
20 21
        })
        
杜庆泉's avatar
杜庆泉 已提交
22 23 24 25 26 27
		test('valueOf', () => {
		    const date1 = new Date('December 17, 1995 03:24:00');
			expect(date1.valueOf()).toEqual(819141840000);
			const date2 = new Date('1995-12-17T03:24:00');
			expect(date2.valueOf()).toEqual(819141840000);
		})
杜庆泉's avatar
杜庆泉 已提交
28 29 30 31
		test('parse', () => {
			const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
			const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');
			
lizhongyi_'s avatar
lizhongyi_ 已提交
32 33
			expect(unixTimeZero).toEqual(0);
			expect(javaScriptRelease).toEqual(818035920000);
杜庆泉's avatar
杜庆泉 已提交
34
		})
杜庆泉's avatar
杜庆泉 已提交
35 36 37 38
		test('toTimeString', () => {
			const date1 = new Date('01 Jan 1970 00:00:00 GMT');
			const date2 = new Date('December 17, 1995 03:24:00');
			
杜庆泉's avatar
杜庆泉 已提交
39
			// #ifdef APP-IOS
杜庆泉's avatar
杜庆泉 已提交
40 41 42
			expect(date1.toTimeString()).toEqual("08:00:00 GMT+0800 (中国标准时间)");
			expect(date2.toTimeString()).toEqual("03:24:00 GMT+0800 (中国标准时间)");
			// #endif
杜庆泉's avatar
杜庆泉 已提交
43
			// #ifdef APP-ANDROID
杜庆泉's avatar
杜庆泉 已提交
44 45 46
			expect(date1.toTimeString()).toEqual("08:00:00 GMT+0800");
			expect(date2.toTimeString()).toEqual("03:24:00 GMT+0800");
			// #endif
杜庆泉's avatar
杜庆泉 已提交
47 48 49
		})
    
    test('toXString', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
50
      // #ifdef APP-ANDROID
杜庆泉's avatar
杜庆泉 已提交
51 52 53 54 55 56 57 58 59
    	const event = new Date('1995-12-17T03:24:00');
      expect(event.toString()).toEqual("Sun Dec 17 1995 03:24:00 GMT+0800");
      expect(event.toTimeString()).toEqual("03:24:00 GMT+0800");
      expect(event.toISOString()).toEqual("1995-12-16T19:24:00.000Z");
      expect(event.toJSON()).toEqual("1995-12-16T19:24:00.000Z");
      expect(event.toDateString()).toEqual("Sun Dec 17 1995");
      
      const event2 = new Date('2014-01-09 22:00:00');
      expect(event2.toString()).toEqual("Thu Jan 09 2014 22:00:00 GMT+0800");
杜庆泉's avatar
杜庆泉 已提交
60
      expect(event2.toTimeString()).toEqual("22:00:00 GMT+0800");
杜庆泉's avatar
杜庆泉 已提交
61
      expect(event2.toISOString()).toEqual("2014-01-09T14:00:00.000Z");
杜庆泉's avatar
杜庆泉 已提交
62 63
      expect(event2.toJSON()).toEqual("2014-01-09T14:00:00.000Z");
      expect(event2.toDateString()).toEqual("Thu Jan 09 2014");
lizhongyi_'s avatar
lizhongyi_ 已提交
64
      // #endif
杜庆泉's avatar
杜庆泉 已提交
65
    })
杜庆泉's avatar
杜庆泉 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
    
    
    test('newDateTest', () => {
      // #ifdef APP-ANDROID
      expect(new Date("2024/5/1").toString()).toEqual("Wed May 01 2024 00:00:00 GMT+0800");
      expect(new Date("2024/5/1 10:00:00").toString()).toEqual("Wed May 01 2024 10:00:00 GMT+0800");
      expect(new Date("2024-05-01 10:00:00").toString()).toEqual("Wed May 01 2024 10:00:00 GMT+0800");
      expect(new Date("2024-05-01 11:00").toString()).toEqual("Wed May 01 2024 11:00:00 GMT+0800");
      expect(new Date("2024/05/01 12:00").toString()).toEqual("Wed May 01 2024 12:00:00 GMT+0800");
      expect(new Date("2024-5-1 10:00").toString()).toEqual("Wed May 01 2024 10:00:00 GMT+0800");
      expect(new Date("2024/5/1 10:00").toString()).toEqual("Wed May 01 2024 10:00:00 GMT+0800");
      // #endif
    })
杜庆泉's avatar
杜庆泉 已提交
79
    
杜庆泉's avatar
杜庆泉 已提交
80
		
Y
yurj26 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        test('getDate', () => {
            const birthday = new Date('August 19, 1975 23:15:30');
            const date1 = birthday.getDate();
            expect(date1).toEqual(19);
            // first millisecond
            expect(new Date(2016, 6, 6).getDate()).toEqual(6);
            // previous millisecond
            expect(new Date(2016, 6, 6, 0, 0, 0, -1).getDate()).toEqual(5);
            // final millisecond
            expect(new Date(2016, 6, 6, 23, 59, 59, 999).getDate()).toEqual(6);
            // subsequent millisecond
            expect(new Date(2016, 6, 6, 23, 59, 59, 1000).getDate()).toEqual(7);

            // first millisecond (month boundary)
            expect(new Date(2016, 1, 29).getDate()).toEqual(29);
            // previous millisecond (month boundary)
            expect(new Date(2016, 1, 29, 0, 0, 0, -1).getDate()).toEqual(28);
            // final millisecond (month boundary)
            expect(new Date(2016, 1, 29, 23, 59, 59, 999).getDate()).toEqual(29);
            // subsequent millisecond (month boundary)
101 102
            expect(new Date(2016, 1, 29, 23, 59, 59, 1000).getDate()).toEqual(1);
            
103 104
            // #ifndef WEB
            // safari 15不支持此格式的日期字符串
105
            expect(Date.parse("2024-01-09 22:00:00")).toEqual(1704808800000);
106
            // #endif
107
            
Y
yurj26 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        })
        test('getDay', () => {
            const birthday = new Date('August 19, 1975 23:15:30');
            const day1 = birthday.getDay();
            expect(day1).toEqual(2);
            // first millisecond
            expect(new Date(2016, 6, 6).getDay()).toEqual(3);
            // previous millisecond
            expect(new Date(2016, 6, 6, 0, 0, 0, -1).getDay()).toEqual(2);
            // final millisecond
            expect(new Date(2016, 6, 6, 23, 59, 59, 999).getDay()).toEqual(3);
            // subsequent millisecond
            expect(new Date(2016, 6, 6, 23, 59, 59, 1000).getDay()).toEqual(4);

            // first millisecond (week boundary)
            expect(new Date(2016, 6, 9).getDay()).toEqual(6);
            // previous millisecond (week boundary)
            expect(new Date(2016, 6, 9, 0, 0, 0, -1).getDay()).toEqual(5);
            // final millisecond (week boundary)
            expect(new Date(2016, 6, 9, 23, 59, 59, 999).getDay()).toEqual(6);
            // subsequent millisecond (week boundary)
            expect(new Date(2016, 6, 9, 23, 59, 59, 1000).getDay()).toEqual(0);
        })
        test('getFullYear', () => {
            const moonLanding = new Date('July 20, 69 00:20:18');
            expect(moonLanding.getFullYear()).toEqual(1969);
            // first millisecond
            expect(new Date(2016, 0).getFullYear()).toEqual(2016);
            // previous millisecond
            expect(new Date(2016, 0, 1, 0, 0, 0, -1).getFullYear()).toEqual(2015);
            // final millisecond
            expect(new Date(2016, 11, 31, 23, 59, 59, 999).getFullYear()).toEqual(2016);
            // subsequent millisecond
            expect(new Date(2016, 11, 31, 23, 59, 59, 1000).getFullYear()).toEqual(2017);
        })
        test('getHours', () => {
            const birthday = new Date('March 13, 08 04:20');
            expect(birthday.getHours()).toEqual(4);
            // first millisecond
            expect(new Date(2016, 6, 6, 13).getHours()).toEqual(13);
            // previous millisecond
            expect(new Date(2016, 6, 6, 13, 0, 0, -1).getHours()).toEqual(12);
            // final millisecond
            expect(new Date(2016, 6, 6, 13, 59, 59, 999).getHours()).toEqual(13);
            // subsequent millisecond
            expect(new Date(2016, 6, 6, 13, 59, 59, 1000).getHours()).toEqual(14);
154 155
        })
        
Y
yurj26 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        test('getMilliseconds', () => {
            const moonLanding = new Date('July 20, 69 00:20:18');
            moonLanding.setMilliseconds(123);
            expect(moonLanding.getMilliseconds()).toEqual(123);

            // first millisecond
            expect(new Date(2016, 6, 6).getMilliseconds()).toEqual(0);
            // previous millisecond
            expect(new Date(2016, 6, 6, 0, 0, 0, -1).getMilliseconds()).toEqual(999);
            // final millisecond
            expect(new Date(2016, 6, 6, 23, 59, 59, 999).getMilliseconds()).toEqual(999);
            // subsequent millisecond
            expect(new Date(2016, 6, 6, 23, 59, 59, 1000).getMilliseconds()).toEqual(0);
        })
        test('getMinutes', () => {
            const birthday = new Date('March 13, 08 04:20');
            expect(birthday.getMinutes()).toEqual(20);
            // first millisecond
            expect(new Date(2016, 6, 6, 14, 6).getMinutes()).toEqual(6);
            // previous millisecond
            expect(new Date(2016, 6, 6, 14, 6, 0, -1).getMinutes()).toEqual(5);
            // final millisecond
            expect(new Date(2016, 6, 6, 14, 6, 59, 999).getMinutes()).toEqual(6);
            // subsequent millisecond
            expect(new Date(2016, 6, 6, 14, 6, 59, 1000).getMinutes()).toEqual(7);
        })
        test('getMonth', () => {
            const moonLanding = new Date('July 20, 69 00:20:18');
            expect(moonLanding.getMonth()).toEqual(6);
            // first millisecond
            expect(new Date(2016, 6).getMonth()).toEqual(6);
            // previous millisecond
            expect(new Date(2016, 6, 0, 0, 0, 0, -1).getMonth()).toEqual(5);
            // final millisecond
            expect(new Date(2016, 6, 31, 23, 59, 59, 999).getMonth()).toEqual(6);
            // subsequent millisecond
            expect(new Date(2016, 6, 31, 23, 59, 59, 1000).getMonth()).toEqual(7);
        })
        test('getSeconds', () => {
            const moonLanding = new Date('July 20, 69 00:20:18');
            expect(moonLanding.getSeconds()).toEqual(18);
            // first millisecond
            expect(new Date(2016, 6, 6, 14, 16, 30).getSeconds()).toEqual(30);
            // previous millisecond
            expect(new Date(2016, 6, 6, 14, 16, 30, -1).getSeconds()).toEqual(29);
            // final millisecond
            expect(new Date(2016, 6, 6, 14, 16, 30, 999).getSeconds()).toEqual(30);
            // subsequent millisecond
            expect(new Date(2016, 6, 6, 14, 16, 30, 1000).getSeconds()).toEqual(31);
        })
        test('getTime', () => {
            const moonLanding = new Date('July 20, 69 20:17:40 GMT+00:00');
            expect(moonLanding.getTime()).toEqual(-14182940000);
            expect(new Date(0).getTime()).toEqual(0);
            expect(new Date(1).getTime()).toEqual(1);
        })
        test('setDate', () => {
            const event = new Date('August 19, 1975 23:15:30');
            event.setDate(24);
            expect(event.getDate()).toEqual(24);
            event.setDate(32);
            expect(event.getDate()).toEqual(1);

            // var date = new Date(2016, 6);
            // let returnValue = date.setDate(6);
            // let expected = new Date(2016, 6, 6).getTime();
            // expect(returnValue).toEqual(expected);
        })
        test('setFullYear', () => {
            const event = new Date('August 19, 1975 23:15:30');
            event.setFullYear(1969);
            expect(event.getFullYear()).toEqual(1969);
        })
        test('setHours', () => {
            const event = new Date('August 19, 1975 23:15:30');
            event.setHours(20);
            expect(event.getHours()).toEqual(20);
        })
        test('setMilliseconds', () => {
            const event = new Date('August 19, 1975 23:15:30');
            expect(event.getMilliseconds()).toEqual(0);
            event.setMilliseconds(456);
            expect(event.getMilliseconds()).toEqual(456);
        })
        test('setMinutes', () => {
            const event = new Date('August 19, 1975 23:15:30');
            event.setMinutes(45);
            expect(event.getMinutes()).toEqual(45);
        })
杜庆泉's avatar
杜庆泉 已提交
245
        test('setMonth', () => {
Y
yurj26 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            const event = new Date('August 19, 1975 23:15:30');
            event.setMonth(3);
            expect(event.getMonth()).toEqual(3);
        })
        test('setSeconds', () => {
            const event = new Date('August 19, 1975 23:15:30');
            event.setSeconds(42);
            expect(event.getSeconds()).toEqual(42);
        })
        test('setTime', () => {
            const launchDate = new Date('July 1, 1999, 12:00:00');
            const futureDate = new Date();
            futureDate.setTime(launchDate.getTime());
            expect(futureDate.getTime()).toEqual(launchDate.getTime());
        })
    })
}