moment-tests.ts 13 KB

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