time.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/ig, hh).replace(/ii/ig, ii).replace(/ss/ig, 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.replace(/yyyy/ig, yyyy).replace(/yy/ig, yy).replace(/mm/ig, mm).replace(/dd/ig, dd);
  43. }
  44. this.execCommand('insertHtml',cmd == "time" ? formatTime(date, format):formatDate(date, format) );
  45. }
  46. };