DateTime格式为3个字母的月份 [英] DateTime formatting 3 letter month

查看:129
本文介绍了DateTime格式为3个字母的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码以这种格式返回日期 - 21-03-2014。

The code below returns the date in this format - 21-03-2014.

我们需要它以这种格式返回日期 - 21-MAR-2014 。三字母文本指定。目前的时间会很好,但不是关键。
$ b

We need it to return the date in this format - 21-MAR-2014. Three letter text designation. Current time would be nice but not critical. What changes need to be made?

function onOpen() {
  var ui = DocumentApp.getUi();
  // Or FormApp or SpreadsheetApp.
  ui.createMenu('Custom Menu')
      .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;
      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.');
  }

}
function pad (str, max) {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}


推荐答案

    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.');
      }

    }

这篇关于DateTime格式为3个字母的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆