moment-tests.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /// <reference path="../ts3.1-typings/moment.d.ts" />
  2. import moment = require('../ts3.1-typings/moment');
  3. moment.parseTwoDigitYear("50");
  4. moment().add('hours', 1).fromNow();
  5. var day = new Date(2011, 9, 16);
  6. var dayWrapper = moment(day);
  7. var otherDay = moment(new Date(2020, 3, 7));
  8. var day1 = moment(1318781876406);
  9. var day2 = moment.unix(1318781876);
  10. var day3 = moment("Dec 25, 1995");
  11. var day4 = moment("12-25-1995", "MM-DD-YYYY");
  12. var day5 = moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
  13. var day6 = moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]);
  14. var now = moment();
  15. var day7 = moment([2010, 1, 14, 15, 25, 50, 125]);
  16. var day8 = moment([2010]);
  17. var day9 = moment([2010, 6]);
  18. var day10 = moment([2010, 6, 10]);
  19. var array = [2010, 1, 14, 15, 25, 50, 125];
  20. var day11 = moment(Date.UTC.apply({}, array));
  21. var day12 = moment.unix(1318781876);
  22. var day13 = moment("/Date(1198908717056-0700)/", true);
  23. var day14 = moment("foobazbar", 'L', true);
  24. moment(null);
  25. moment(undefined);
  26. moment({ years: 2010, months: 3, days: 5, hours: 15, minutes: 10, seconds: 3, milliseconds: 123 });
  27. moment("20140101", "YYYYMMDD", true);
  28. moment("20140101", "YYYYMMDD", "en");
  29. moment("20140101", "YYYYMMDD", "en", true);
  30. moment("20140101", ["YYYYMMDD"], true);
  31. moment("20140101", ["YYYYMMDD"], "en");
  32. moment("20140101", ["YYYYMMDD"], "en", true);
  33. moment(day.toISOString(), moment.ISO_8601);
  34. moment(day.toISOString(), moment.ISO_8601, true);
  35. moment(day.toISOString(), moment.ISO_8601, "en", true);
  36. moment(day.toISOString(), [moment.ISO_8601]);
  37. moment(day.toISOString(), [moment.ISO_8601], true);
  38. moment(day.toISOString(), [moment.ISO_8601], "en", true);
  39. moment(day.toUTCString(), moment.RFC_2822);
  40. moment(day.toUTCString(), moment.RFC_2822, true);
  41. moment(day.toUTCString(), moment.RFC_2822, "en", true);
  42. moment(day.toUTCString(), [moment.RFC_2822]);
  43. moment(day.toUTCString(), [moment.RFC_2822], true);
  44. moment(day.toUTCString(), [moment.RFC_2822], "en", true);
  45. var a = moment([2012]);
  46. var b = moment(a);
  47. a.year(2000);
  48. b.year(); // 2012
  49. moment.utc();
  50. moment.utc(12345);
  51. moment.utc([12, 34, 56]);
  52. moment.utc({ years: 2010, months: 3, days: 5, hours: 15, minutes: 10, seconds: 3, milliseconds: 123 });
  53. moment.utc("1-2-3");
  54. moment.utc("1-2-3", "3-2-1");
  55. moment.utc("1-2-3", "3-2-1", true);
  56. moment.utc("1-2-3", "3-2-1", "en");
  57. moment.utc("1-2-3", "3-2-1", "en", true);
  58. moment.utc("01-01-2014", ["DD-MM-YYYY", "MM-DD-YYYY"]);
  59. moment.utc("01-01-2014", ["DD-MM-YYYY", "MM-DD-YYYY"], true);
  60. moment.utc("01-01-2014", ["DD-MM-YYYY", "MM-DD-YYYY"], "en");
  61. moment.utc("01-01-2014", ["DD-MM-YYYY", "MM-DD-YYYY"], "en", true);
  62. var a2 = moment.utc([2011, 0, 1, 8]);
  63. a.hours();
  64. a.local();
  65. a.hours();
  66. moment("2011-10-10", "YYYY-MM-DD").isValid();
  67. moment("2011-10-50", "YYYY-MM-DD").isValid();
  68. moment("2011-10-10T10:20:90").isValid();
  69. moment([2011, 0, 1]).isValid();
  70. moment([2011, 0, 50]).isValid();
  71. moment("not a date").isValid();
  72. moment().add('days', 7).subtract('months', 1).year(2009).hours(0).minutes(0).seconds(0);
  73. moment().add('days', 7);
  74. moment().add('days', 7).add('months', 1);
  75. moment().add({days:7,months:1});
  76. moment().add('milliseconds', 1000000);
  77. moment().add('days', 360);
  78. moment([2010, 0, 31]);
  79. moment([2010, 0, 31]).add('months', 1);
  80. var m = moment(new Date(2011, 2, 12, 5, 0, 0));
  81. m.hours();
  82. m.add('days', 1).hours();
  83. var m2 = moment(new Date(2011, 2, 12, 5, 0, 0));
  84. m2.hours();
  85. m2.add('hours', 24).hours();
  86. var duration = moment.duration({'days': 1});
  87. moment([2012, 0, 31]).add(duration);
  88. moment().add('seconds', 1);
  89. moment().add(1, 'seconds');
  90. moment().add('1', 'seconds');
  91. moment().subtract('days', 7);
  92. moment().seconds(30);
  93. moment().minutes(30);
  94. moment().hours(12);
  95. moment().date(5);
  96. moment().day(5);
  97. moment().day("Sunday");
  98. moment().month(5);
  99. moment().month("January");
  100. moment().year(1984);
  101. moment().startOf('year');
  102. moment().month(0).date(1).hours(0).minutes(0).seconds(0).milliseconds(0);
  103. moment().startOf('hour');
  104. moment().minutes(0).seconds(0).milliseconds(0);
  105. moment().weekday();
  106. moment().weekday(0);
  107. moment().isoWeekday(1);
  108. moment().isoWeekday();
  109. moment().weekYear(2);
  110. moment().weekYear();
  111. moment().isoWeekYear(3);
  112. moment().isoWeekYear();
  113. moment().week();
  114. moment().week(45);
  115. moment().weeks();
  116. moment().weeks(45);
  117. moment().isoWeek();
  118. moment().isoWeek(45);
  119. moment().isoWeeks();
  120. moment().isoWeeks(45);
  121. moment().dayOfYear();
  122. moment().dayOfYear(45);
  123. moment().weeksInYear();
  124. moment().isoWeeksInYear();
  125. moment().isoWeeksInISOWeekYear();
  126. moment().set('year', 2013);
  127. moment().set('month', 3); // April
  128. moment().set('date', 1);
  129. moment().set('hour', 13);
  130. moment().set('minute', 20);
  131. moment().set('second', 30);
  132. moment().set('millisecond', 123);
  133. moment().set({'year': 2013, 'month': 3});
  134. var getMilliseconds: number = moment().milliseconds();
  135. var getSeconds: number = moment().seconds();
  136. var getMinutes: number = moment().minutes();
  137. var getHours: number = moment().hours();
  138. var getDate: number = moment().date();
  139. var getDay: number = moment().day();
  140. var getMonth: number = moment().month();
  141. var getQuater: number = moment().quarter();
  142. var getYear: number = moment().year();
  143. var date: [number, number, number, number, number, number, number] = moment().toArray();
  144. moment().hours(0).minutes(0).seconds(0).milliseconds(0);
  145. var a3 = moment([2011, 0, 1, 8]);
  146. a3.hours();
  147. a3.utc();
  148. a3.hours();
  149. var a4 = moment([2010, 1, 14, 15, 25, 50, 125]);
  150. a4.format("dddd, MMMM Do YYYY, h:mm:ss a");
  151. a4.format("ddd, hA");
  152. moment().format('\\L');
  153. moment().format('[today] DDDD');
  154. var a5 = moment([2007, 0, 29]);
  155. var b5 = moment([2007, 0, 28]);
  156. a5.from(b5);
  157. var a6 = moment([2007, 0, 29]);
  158. var b6 = moment([2007, 0, 28]);
  159. a6.from(b6);
  160. a6.from([2007, 0, 28]);
  161. a6.from(new Date(2007, 0, 28));
  162. a6.from("1-28-2007");
  163. var a7 = moment();
  164. var b7 = moment("10-10-1900", "MM-DD-YYYY");
  165. a7.from(b7);
  166. var start = moment([2007, 0, 5]);
  167. var end = moment([2007, 0, 10]);
  168. start.from(end);
  169. start.from(end, true);
  170. moment([2007, 0, 29]).fromNow();
  171. moment([2007, 0, 29]).fromNow();
  172. moment([2007, 0, 29]).fromNow(true);
  173. var a8 = moment([2007, 0, 29]);
  174. var b8 = moment([2007, 0, 28]);
  175. a8.diff(b8) ;
  176. a8.diff(b8, 'days');
  177. a8.diff(b8, 'years')
  178. a8.diff(b8, 'years', true);
  179. moment.min([a8, b8]);
  180. moment.min(a8, b8);
  181. moment.max([a8, b8]);
  182. moment.max(a8, b8);
  183. moment([2007, 0, 29]).toDate();
  184. moment([2007, 1, 23]).toISOString();
  185. moment(1318874398806).valueOf();
  186. moment(1318874398806).unix();
  187. moment([2000]).isLeapYear();
  188. moment().zone();
  189. moment().utcOffset();
  190. moment("2012-2", "YYYY-MM").daysInMonth();
  191. moment([2011, 2, 12]).isDST();
  192. moment.isMoment(new Date());
  193. moment.isMoment(moment());
  194. moment.isDate(new Date());
  195. moment.isDate(/regexp/);
  196. moment.isDuration(new Date());
  197. moment.isDuration(moment.duration());
  198. moment().isBetween(moment(), moment());
  199. moment().isBetween(new Date(), new Date());
  200. moment().isBetween([1,1,2000], [1,1,2001], "year");
  201. moment().isBetween([1,1,2000], [1,1,2001], null, "()");
  202. moment.localeData('fr');
  203. moment(1316116057189).fromNow();
  204. moment.localeData('en');
  205. var globalLang = moment();
  206. var localLang = moment();
  207. var thresholds = {
  208. ss: 44,
  209. s: 45,
  210. m: 45,
  211. h: 22,
  212. d: 26,
  213. w: 2,
  214. M: 11,
  215. };
  216. localLang.localeData();
  217. localLang.format('LLLL');
  218. globalLang.format('LLLL');
  219. moment.duration(null);
  220. moment.duration(undefined);
  221. moment.duration(100);
  222. moment.duration(2, 'seconds');
  223. moment.duration({
  224. seconds: 2,
  225. minutes: 2,
  226. hours: 2,
  227. days: 2,
  228. weeks: 2,
  229. months: 2,
  230. years: 2
  231. });
  232. moment.duration({
  233. s: 2,
  234. m: 2,
  235. h: 2,
  236. d: 2,
  237. w: 2,
  238. M: 2,
  239. y: 2,
  240. });
  241. moment.duration(1, "minute").clone();
  242. moment.duration(1, "minutes").humanize();
  243. moment.duration(1, "minutes").humanize(true);
  244. moment.duration(1, "minutes").humanize(thresholds);
  245. moment.duration(1, "minutes").humanize(true, thresholds);
  246. moment.duration(500).milliseconds();
  247. moment.duration(500).asMilliseconds();
  248. moment.duration(500).seconds();
  249. moment.duration(500).asSeconds();
  250. moment.duration().minutes();
  251. moment.duration().asMinutes();
  252. moment.duration().toISOString();
  253. moment.duration().toJSON();
  254. var adur = moment.duration(3, 'd');
  255. var bdur = moment.duration(2, 'd');
  256. adur.subtract(bdur).days();
  257. adur.subtract(1).days();
  258. adur.subtract(1, 'd').days();
  259. var calendarFormat = {
  260. sameDay: '[Today]',
  261. nextDay: '[Tomorrow]',
  262. nextWeek: 'dddd',
  263. lastDay: '[Yesterday]',
  264. lastWeek: '[Last] dddd',
  265. sameElse: 'DD/MM/YYYY'
  266. };
  267. moment().calendar();
  268. moment().calendar(null);
  269. moment().calendar(calendarFormat);
  270. moment().calendar(null, calendarFormat);
  271. // Selecting a language
  272. moment.locale();
  273. moment.locale('en');
  274. moment.locale(['en', 'fr']);
  275. moment.defineLocale('en', null);
  276. moment.updateLocale('en', null);
  277. moment.locale('en', null);
  278. // Defining a custom language:
  279. moment.locale('en', {
  280. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  281. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  282. weekdays: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  283. weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  284. weekdaysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
  285. longDateFormat: {
  286. LTS: "h:mm:ss A",
  287. LT: "h:mm A",
  288. L: "MM/DD/YYYY",
  289. LL: "MMMM D YYYY",
  290. LLL: "MMMM D YYYY LT",
  291. LLLL: "dddd, MMMM D YYYY LT"
  292. },
  293. relativeTime: {
  294. future: "in %s",
  295. past: "%s ago",
  296. s: "seconds",
  297. ss: "%d seconds",
  298. m: "a minute",
  299. mm: "%d minutes",
  300. h: "an hour",
  301. hh: "%d hours",
  302. d: "a day",
  303. dd: "%d days",
  304. w: "%d weeks",
  305. M: "a month",
  306. MM: "%d months",
  307. y: "a year",
  308. yy: "%d years"
  309. },
  310. meridiem: function (hour: number, minute: number, isLower: boolean) {
  311. if (hour < 9) {
  312. return "??";
  313. } else if (hour < 11 && minute < 30) {
  314. return "??";
  315. } else if (hour < 13 && minute < 30) {
  316. return "??";
  317. } else if (hour < 18) {
  318. return "??";
  319. } else {
  320. return "??";
  321. }
  322. },
  323. calendar: {
  324. lastDay: '[Yesterday at] LT',
  325. sameDay: '[Today at] LT',
  326. nextDay: '[Tomorrow at] LT',
  327. lastWeek: '[last] dddd [at] LT',
  328. nextWeek: 'dddd [at] LT',
  329. sameElse: 'L'
  330. },
  331. ordinal: function (number: number) {
  332. var b = number % 10;
  333. return (~~(number % 100 / 10) === 1) ? 'th' :
  334. (b === 1) ? 'st' :
  335. (b === 2) ? 'nd' :
  336. (b === 3) ? 'rd' : 'th';
  337. },
  338. week: {
  339. dow: 1,
  340. doy: 4
  341. }
  342. });
  343. moment.locale('en', {
  344. months : [
  345. "January", "February", "March", "April", "May", "June", "July",
  346. "August", "September", "October", "November", "December"
  347. ]
  348. });
  349. moment.locale('en', {
  350. months : function (momentToFormat: moment.Moment, format: string) {
  351. // momentToFormat is the moment currently being formatted
  352. // format is the formatting string
  353. if (/^MMMM/.test(format)) { // if the format starts with 'MMMM'
  354. return this.nominative[momentToFormat.month()];
  355. } else {
  356. return this.subjective[momentToFormat.month()];
  357. }
  358. }
  359. });
  360. moment.locale('en', {
  361. monthsShort : [
  362. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  363. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  364. ]
  365. });
  366. moment.locale('en', {
  367. monthsShort : function (momentToFormat: moment.Moment, format: string) {
  368. if (/^MMMM/.test(format)) {
  369. return this.nominative[momentToFormat.month()];
  370. } else {
  371. return this.subjective[momentToFormat.month()];
  372. }
  373. }
  374. });
  375. moment.locale('en', {
  376. weekdays : [
  377. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  378. ]
  379. });
  380. moment.locale('en', {
  381. weekdays : function (momentToFormat: moment.Moment) {
  382. return this.weekdays[momentToFormat.day()];
  383. }
  384. });
  385. moment.locale('en', {
  386. weekdaysShort : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
  387. });
  388. moment.locale('en', {
  389. weekdaysShort : function (momentToFormat: moment.Moment) {
  390. return this.weekdaysShort[momentToFormat.day()];
  391. }
  392. });
  393. moment.locale('en', {
  394. weekdaysMin : ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
  395. });
  396. moment.locale('en', {
  397. weekdaysMin : function (momentToFormat: moment.Moment) {
  398. return this.weekdaysMin[momentToFormat.day()];
  399. }
  400. });
  401. moment.locale('en', {
  402. longDateFormat : {
  403. LTS: "h:mm:ss A",
  404. LT: "h:mm A",
  405. L: "MM/DD/YYYY",
  406. l: "M/D/YYYY",
  407. LL: "MMMM Do YYYY",
  408. ll: "MMM D YYYY",
  409. LLL: "MMMM Do YYYY LT",
  410. lll: "MMM D YYYY LT",
  411. LLLL: "dddd, MMMM Do YYYY LT",
  412. llll: "ddd, MMM D YYYY LT"
  413. }
  414. });
  415. moment.locale('en', {
  416. longDateFormat : {
  417. LTS: "h:mm A",
  418. LT: "h:mm A",
  419. L: "MM/DD/YYYY",
  420. LL: "MMMM Do YYYY",
  421. LLL: "MMMM Do YYYY LT",
  422. LLLL: "dddd, MMMM Do YYYY LT"
  423. }
  424. });
  425. moment.locale('en', {
  426. relativeTime : {
  427. future: "in %s",
  428. past: "%s ago",
  429. s: "seconds",
  430. ss: "%d seconds",
  431. m: "a minute",
  432. mm: "%d minutes",
  433. h: "an hour",
  434. hh: "%d hours",
  435. d: "a day",
  436. dd: "%d days",
  437. M: "a month",
  438. MM: "%d months",
  439. y: "a year",
  440. yy: "%d years"
  441. }
  442. });
  443. moment.locale('en', {
  444. meridiem : function (hour: number, minute: number, isLowercase: boolean) {
  445. if (hour < 9) {
  446. return "早上";
  447. } else if (hour < 11 && minute < 30) {
  448. return "上午";
  449. } else if (hour < 13 && minute < 30) {
  450. return "中午";
  451. } else if (hour < 18) {
  452. return "下午";
  453. } else {
  454. return "晚上";
  455. }
  456. }
  457. });
  458. moment.locale('en', {
  459. calendar : {
  460. lastDay : '[Yesterday at] LT',
  461. sameDay : '[Today at] LT',
  462. nextDay : function () {
  463. return '[hoy a la' + ((this.hours() !== 1) ? 's' : '') + '] LT';
  464. },
  465. lastWeek : '[last] dddd [at] LT',
  466. nextWeek : 'dddd [at] LT',
  467. sameElse : 'L'
  468. }
  469. });
  470. moment.locale('en', {
  471. ordinal : function (number: number) {
  472. var b = number % 10;
  473. var output = (~~ (number % 100 / 10) === 1) ? 'th' :
  474. (b === 1) ? 'st' :
  475. (b === 2) ? 'nd' :
  476. (b === 3) ? 'rd' : 'th';
  477. return number + output;
  478. }
  479. });
  480. // console.log(moment.version);
  481. moment.defaultFormat = 'YYYY-MM-DD HH:mm';
  482. moment.suppressDeprecationWarnings = true;
  483. moment.deprecationHandler = null;
  484. moment.deprecationHandler = undefined;
  485. moment.deprecationHandler = function(name: string | null, msg: string) {}