以下代码以此格式返回日期 - 21-03-2014。
我们需要它以这种格式返回日期 - 2014年3月21日。三个字母的文字指定。当前时间不错但不重要。需要做出哪些改变?
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;
}
答案 0 :(得分:2)
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.');
}
}
答案 1 :(得分:0)
或者:
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;
//}