moment-tests.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. moment().hours(0).minutes(0).seconds(0).milliseconds(0);
  144. var a3 = moment([2011, 0, 1, 8]);
  145. a3.hours();
  146. a3.utc();
  147. a3.hours();
  148. var a4 = moment([2010, 1, 14, 15, 25, 50, 125]);
  149. a4.format("dddd, MMMM Do YYYY, h:mm:ss a");
  150. a4.format("ddd, hA");
  151. moment().format('\\L');
  152. moment().format('[today] DDDD');
  153. var a5 = moment([2007, 0, 29]);
  154. var b5 = moment([2007, 0, 28]);
  155. a5.from(b5);
  156. var a6 = moment([2007, 0, 29]);
  157. var b6 = moment([2007, 0, 28]);
  158. a6.from(b6);
  159. a6.from([2007, 0, 28]);
  160. a6.from(new Date(2007, 0, 28));
  161. a6.from("1-28-2007");
  162. var a7 = moment();
  163. var b7 = moment("10-10-1900", "MM-DD-YYYY");
  164. a7.from(b7);
  165. var start = moment([2007, 0, 5]);
  166. var end = moment([2007, 0, 10]);
  167. start.from(end);
  168. start.from(end, true);
  169. moment([2007, 0, 29]).fromNow();
  170. moment([2007, 0, 29]).fromNow();
  171. moment([2007, 0, 29]).fromNow(true);
  172. var a8 = moment([2007, 0, 29]);
  173. var b8 = moment([2007, 0, 28]);
  174. a8.diff(b8) ;
  175. a8.diff(b8, 'days');
  176. a8.diff(b8, 'years')
  177. a8.diff(b8, 'years', true);
  178. moment.min([a8, b8]);
  179. moment.min(a8, b8);
  180. moment.max([a8, b8]);
  181. moment.max(a8, b8);
  182. moment([2007, 0, 29]).toDate();
  183. moment([2007, 1, 23]).toISOString();
  184. moment(1318874398806).valueOf();
  185. moment(1318874398806).unix();
  186. moment([2000]).isLeapYear();
  187. moment().zone();
  188. moment().utcOffset();
  189. moment("2012-2", "YYYY-MM").daysInMonth();
  190. moment([2011, 2, 12]).isDST();
  191. moment.isMoment(new Date());
  192. moment.isMoment(moment());
  193. moment.isDate(new Date());
  194. moment.isDate(/regexp/);
  195. moment.isDuration(new Date());
  196. moment.isDuration(moment.duration());
  197. moment().isBetween(moment(), moment());
  198. moment().isBetween(new Date(), new Date());
  199. moment().isBetween([1,1,2000], [1,1,2001], "year");
  200. moment().isBetween([1,1,2000], [1,1,2001], null, "()");
  201. moment.localeData('fr');
  202. moment(1316116057189).fromNow();
  203. moment.localeData('en');
  204. var globalLang = moment();
  205. var localLang = moment();
  206. var thresholds = {
  207. ss: 44,
  208. s: 45,
  209. m: 45,
  210. h: 22,
  211. d: 26,
  212. w: 2,
  213. M: 11,
  214. };
  215. localLang.localeData();
  216. localLang.format('LLLL');
  217. globalLang.format('LLLL');
  218. moment.duration(null);
  219. moment.duration(undefined);
  220. moment.duration(100);
  221. moment.duration(2, 'seconds');
  222. moment.duration({
  223. seconds: 2,
  224. minutes: 2,
  225. hours: 2,
  226. days: 2,
  227. weeks: 2,
  228. months: 2,
  229. years: 2
  230. });
  231. moment.duration({
  232. s: 2,
  233. m: 2,
  234. h: 2,
  235. d: 2,
  236. w: 2,
  237. M: 2,
  238. y: 2,
  239. });
  240. moment.duration(1, "minute").clone();
  241. moment.duration(1, "minutes").humanize();
  242. moment.duration(1, "minutes").humanize(true);
  243. moment.duration(1, "minutes").humanize(thresholds);
  244. moment.duration(1, "minutes").humanize(true, thresholds);
  245. moment.duration(500).milliseconds();
  246. moment.duration(500).asMilliseconds();
  247. moment.duration(500).seconds();
  248. moment.duration(500).asSeconds();
  249. moment.duration().minutes();
  250. moment.duration().asMinutes();
  251. moment.duration().toISOString();
  252. moment.duration().toJSON();
  253. var adur = moment.duration(3, 'd');
  254. var bdur = moment.duration(2, 'd');
  255. adur.subtract(bdur).days();
  256. adur.subtract(1).days();
  257. adur.subtract(1, 'd').days();
  258. var calendarFormat = {
  259. sameDay: '[Today]',
  260. nextDay: '[Tomorrow]',
  261. nextWeek: 'dddd',
  262. lastDay: '[Yesterday]',
  263. lastWeek: '[Last] dddd',
  264. sameElse: 'DD/MM/YYYY'
  265. };
  266. moment().calendar();
  267. moment().calendar(null);
  268. moment().calendar(calendarFormat);
  269. moment().calendar(null, calendarFormat);
  270. // Selecting a language
  271. moment.locale();
  272. moment.locale('en');
  273. moment.locale(['en', 'fr']);
  274. moment.defineLocale('en', null);
  275. moment.updateLocale('en', null);
  276. moment.locale('en', null);
  277. // Defining a custom language:
  278. moment.locale('en', {
  279. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  280. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  281. weekdays: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  282. weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  283. weekdaysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
  284. longDateFormat: {
  285. LTS: "h:mm:ss A",
  286. LT: "h:mm A",
  287. L: "MM/DD/YYYY",
  288. LL: "MMMM D YYYY",
  289. LLL: "MMMM D YYYY LT",
  290. LLLL: "dddd, MMMM D YYYY LT"
  291. },
  292. relativeTime: {
  293. future: "in %s",
  294. past: "%s ago",
  295. s: "seconds",
  296. ss: "%d seconds",
  297. m: "a minute",
  298. mm: "%d minutes",
  299. h: "an hour",
  300. hh: "%d hours",
  301. d: "a day",
  302. dd: "%d days",
  303. w: "%d weeks",
  304. M: "a month",
  305. MM: "%d months",
  306. y: "a year",
  307. yy: "%d years"
  308. },
  309. meridiem: function (hour: number, minute: number, isLower: boolean) {
  310. if (hour < 9) {
  311. return "??";
  312. } else if (hour < 11 && minute < 30) {
  313. return "??";
  314. } else if (hour < 13 && minute < 30) {
  315. return "??";
  316. } else if (hour < 18) {
  317. return "??";
  318. } else {
  319. return "??";
  320. }
  321. },
  322. calendar: {
  323. lastDay: '[Yesterday at] LT',
  324. sameDay: '[Today at] LT',
  325. nextDay: '[Tomorrow at] LT',
  326. lastWeek: '[last] dddd [at] LT',
  327. nextWeek: 'dddd [at] LT',
  328. sameElse: 'L'
  329. },
  330. ordinal: function (number: number) {
  331. var b = number % 10;
  332. return (~~(number % 100 / 10) === 1) ? 'th' :
  333. (b === 1) ? 'st' :
  334. (b === 2) ? 'nd' :
  335. (b === 3) ? 'rd' : 'th';
  336. },
  337. week: {
  338. dow: 1,
  339. doy: 4
  340. }
  341. });
  342. moment.locale('en', {
  343. months : [
  344. "January", "February", "March", "April", "May", "June", "July",
  345. "August", "September", "October", "November", "December"
  346. ]
  347. });
  348. moment.locale('en', {
  349. months : function (momentToFormat: moment.Moment, format: string) {
  350. // momentToFormat is the moment currently being formatted
  351. // format is the formatting string
  352. if (/^MMMM/.test(format)) { // if the format starts with 'MMMM'
  353. return this.nominative[momentToFormat.month()];
  354. } else {
  355. return this.subjective[momentToFormat.month()];
  356. }
  357. }
  358. });
  359. moment.locale('en', {
  360. monthsShort : [
  361. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  362. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  363. ]
  364. });
  365. moment.locale('en', {
  366. monthsShort : function (momentToFormat: moment.Moment, format: string) {
  367. if (/^MMMM/.test(format)) {
  368. return this.nominative[momentToFormat.month()];
  369. } else {
  370. return this.subjective[momentToFormat.month()];
  371. }
  372. }
  373. });
  374. moment.locale('en', {
  375. weekdays : [
  376. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  377. ]
  378. });
  379. moment.locale('en', {
  380. weekdays : function (momentToFormat: moment.Moment) {
  381. return this.weekdays[momentToFormat.day()];
  382. }
  383. });
  384. moment.locale('en', {
  385. weekdaysShort : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
  386. });
  387. moment.locale('en', {
  388. weekdaysShort : function (momentToFormat: moment.Moment) {
  389. return this.weekdaysShort[momentToFormat.day()];
  390. }
  391. });
  392. moment.locale('en', {
  393. weekdaysMin : ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
  394. });
  395. moment.locale('en', {
  396. weekdaysMin : function (momentToFormat: moment.Moment) {
  397. return this.weekdaysMin[momentToFormat.day()];
  398. }
  399. });
  400. moment.locale('en', {
  401. longDateFormat : {
  402. LTS: "h:mm:ss A",
  403. LT: "h:mm A",
  404. L: "MM/DD/YYYY",
  405. l: "M/D/YYYY",
  406. LL: "MMMM Do YYYY",
  407. ll: "MMM D YYYY",
  408. LLL: "MMMM Do YYYY LT",
  409. lll: "MMM D YYYY LT",
  410. LLLL: "dddd, MMMM Do YYYY LT",
  411. llll: "ddd, MMM D YYYY LT"
  412. }
  413. });
  414. moment.locale('en', {
  415. longDateFormat : {
  416. LTS: "h:mm A",
  417. LT: "h:mm A",
  418. L: "MM/DD/YYYY",
  419. LL: "MMMM Do YYYY",
  420. LLL: "MMMM Do YYYY LT",
  421. LLLL: "dddd, MMMM Do YYYY LT"
  422. }
  423. });
  424. moment.locale('en', {
  425. relativeTime : {
  426. future: "in %s",
  427. past: "%s ago",
  428. s: "seconds",
  429. ss: "%d seconds",
  430. m: "a minute",
  431. mm: "%d minutes",
  432. h: "an hour",
  433. hh: "%d hours",
  434. d: "a day",
  435. dd: "%d days",
  436. M: "a month",
  437. MM: "%d months",
  438. y: "a year",
  439. yy: "%d years"
  440. }
  441. });
  442. moment.locale('en', {
  443. meridiem : function (hour: number, minute: number, isLowercase: boolean) {
  444. if (hour < 9) {
  445. return "早上";
  446. } else if (hour < 11 && minute < 30) {
  447. return "上午";
  448. } else if (hour < 13 && minute < 30) {
  449. return "中午";
  450. } else if (hour < 18) {
  451. return "下午";
  452. } else {
  453. return "晚上";
  454. }
  455. }
  456. });
  457. moment.locale('en', {
  458. calendar : {
  459. lastDay : '[Yesterday at] LT',
  460. sameDay : '[Today at] LT',
  461. nextDay : function () {
  462. return '[hoy a la' + ((this.hours() !== 1) ? 's' : '') + '] LT';
  463. },
  464. lastWeek : '[last] dddd [at] LT',
  465. nextWeek : 'dddd [at] LT',
  466. sameElse : 'L'
  467. }
  468. });
  469. moment.locale('en', {
  470. ordinal : function (number: number) {
  471. var b = number % 10;
  472. var output = (~~ (number % 100 / 10) === 1) ? 'th' :
  473. (b === 1) ? 'st' :
  474. (b === 2) ? 'nd' :
  475. (b === 3) ? 'rd' : 'th';
  476. return number + output;
  477. }
  478. });
  479. // console.log(moment.version);
  480. moment.defaultFormat = 'YYYY-MM-DD HH:mm';
  481. moment.suppressDeprecationWarnings = true;
  482. moment.deprecationHandler = null;
  483. moment.deprecationHandler = undefined;
  484. moment.deprecationHandler = function(name: string | null, msg: string) {}