time.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 插入时间和日期
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 插入时间,默认格式:12:59:59
  8. * @command time
  9. * @method execCommand
  10. * @param { String } cmd 命令字符串
  11. * @example
  12. * ```javascript
  13. * editor.execCommand( 'time');
  14. * ```
  15. */
  16. /**
  17. * 插入日期,默认格式:2013-08-30
  18. * @command date
  19. * @method execCommand
  20. * @param { String } cmd 命令字符串
  21. * @example
  22. * ```javascript
  23. * editor.execCommand( 'date');
  24. * ```
  25. */
  26. UE.commands["time"] = UE.commands["date"] = {
  27. execCommand: function(cmd, format) {
  28. var date = new Date();
  29. function formatTime(date, format) {
  30. var hh = ("0" + date.getHours()).slice(-2),
  31. ii = ("0" + date.getMinutes()).slice(-2),
  32. ss = ("0" + date.getSeconds()).slice(-2);
  33. format = format || "hh:ii:ss";
  34. return format.replace(/hh/gi, hh).replace(/ii/gi, ii).replace(/ss/gi, ss);
  35. }
  36. function formatDate(date, format) {
  37. var yyyy = ("000" + date.getFullYear()).slice(-4),
  38. yy = yyyy.slice(-2),
  39. mm = ("0" + (date.getMonth() + 1)).slice(-2),
  40. dd = ("0" + date.getDate()).slice(-2);
  41. format = format || "yyyy-mm-dd";
  42. return format
  43. .replace(/yyyy/gi, yyyy)
  44. .replace(/yy/gi, yy)
  45. .replace(/mm/gi, mm)
  46. .replace(/dd/gi, dd);
  47. }
  48. this.execCommand(
  49. "insertHtml",
  50. cmd == "time" ? formatTime(date, format) : formatDate(date, format)
  51. );
  52. }
  53. };