DateTime formatting 3 letter month

血红的双手。 提交于 2019-12-02 11:34:01
    function insertDate() {
      var cursor = DocumentApp.getActiveDocument().getCursor();
      if (cursor) {
          // Attempt to insert text at the cursor position. If insertion returns null,
          // then the cursor's containing element doesn't allow text insertions.
          var month=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
          var d = new Date();
          var dd = d.getDate();
          dd = pad(dd, 2)
          var mm = d.getMonth();
          mm = pad(mm, 2)
          var yyyy = d.getFullYear();
          var date = dd + "-" + month[mm] + "-" + yyyy;
          var element = cursor.insertText(date);
          if (element) {
            element.setBold(true);
          } else {
            DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
          }
        } else {
          DocumentApp.getUi().alert('Cannot find a cursor in the document.');
      }

    }

Or:

    function onOpen() {
  var ui = DocumentApp.getUi();
  // Or FormApp or SpreadsheetApp.
  ui.createMenu('Date/Time')
      .addItem('Insert Date', 'insertDate')
      .addToUi();

}

function insertDate() {
  var cursor = DocumentApp.getActiveDocument().getCursor();
  if (cursor) {
      // Attempt to insert text at the cursor position. If insertion returns null,
      // then the cursor's containing element doesn't allow text insertions.
//      var d = new Date();
//      var dd = d.getDate();
//      dd = pad(dd, 2)
//      var mm = d.getMonth() + 1; //Months are zero based
//      mm = pad(mm, 2)
//      var yyyy = d.getFullYear();
//      var date = dd + "-" + mm + "-" + yyyy;

//////////////////SEE THIS ONE LINE BELOW/////////////////////////////////
      var element = cursor.insertText(Utilities.formatDate(new Date(), "PST", "MMM MM-dd-yyyy hh:mm a"));
//////////////////SEE THIS ONE LINE ABOVE/////////////////////////////////

      if (element) {
        element.setBold(true);
      } else {
        DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
      }
    } else {
      DocumentApp.getUi().alert('Cannot find a cursor in the document.');
  }

}
//function pad (str, max) {
//  str = str.toString();
//  return str.length < max ? pad("0" + str, max) : str;
//}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!