Skip to content

Instantly share code, notes, and snippets.

@pbaruns

pbaruns/DateTime

Last active Jun 11, 2021
Embed
What would you like to do?
Insert Date Time
/**
* The onOpen function runs automatically when the Google Docs document is
* opened. Use it to add custom menus to Google Docs that allow the user to run
* custom scripts. For more information, please consult the following two
* resources.
*
* Extending Google Docs developer guide:
* https://developers.google.com/apps-script/guides/docs
*
* Document service reference documentation:
* https://developers.google.com/apps-script/reference/document/
*/
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('Utilities')
.addItem('Insert Date', 'insertAtCursor')
.addItem('Insert Time', 'insertTimeAtCursor')
.addItem('Insert Long Date', 'insertLDateAtCursor')
.addItem('Insert Date and Time', 'insertDTAtCursor')
.addItem('Insert Custom Long Date and Time', 'insertCLDTAtCursor')
.addToUi();
}
/**
* Inserts the date at the current cursor location in boldface.
*/
function insertAtCursor() {
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 date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
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.');
}
moveCursorToEnd();
}
/**
* Inserts the time at the current cursor location in boldface.
*/
function insertTimeAtCursor() {
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 time = Utilities.formatDate(new Date(), "GMT", "HH:mm:ss"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(time);
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.');
}
moveCursorToEnd();
}
/**
* Inserts the long date at the current cursor location in boldface.
*/
function insertLDateAtCursor() {
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 longDate = Utilities.formatDate(new Date(), "GMT", "MMMM dd, yyyy"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(longDate);
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.');
}
moveCursorToEnd();
}
/**
* Inserts the Date and time at the current cursor location in boldface.
*/
function insertDTAtCursor() {
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 longDateTime = Utilities.formatDate(new Date(), "GMT", "MMMM dd, yyyy HH:mm"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(longDateTime);
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.');
}
moveCursorToEnd();
}
/**
* Inserts the custom long date and time at the current cursor location in boldface.
*/
function insertCLDTAtCursor() {
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 customLDateTime = Utilities.formatDate(new Date(), "GMT", "EEEE, MMMM dd, yyyy- HH:mm'hrs'"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var element = cursor.insertText(customLDateTime);
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.');
}
moveCursorToEnd();
}
function moveCursorToEnd() {
//move cursor to the end
var doc=DocumentApp.getActiveDocument();
var txtElement=doc.getCursor().getElement();
var txtOffset=doc.getCursor().getOffset();
var position=doc.newPosition(txtElement, txtOffset + 1);
doc.setCursor(position);
}
@pbaruns

This comment has been minimized.

Copy link
Owner Author

@pbaruns pbaruns commented Dec 16, 2018

Added 'time' and 'long date' formats.
Change the timezone as per your needs from 'GMT' to your time zone abbr.

@pbaruns

This comment has been minimized.

Copy link
Owner Author

@pbaruns pbaruns commented Mar 4, 2019

Added date AND time as a new menu item.

@gabefreitas

This comment has been minimized.

Copy link

@gabefreitas gabefreitas commented Mar 12, 2019

it seems to be glitchy when I change the time zone from EST to EDT time zone to show Daylight Savings time. Or am I missing something in the code?

@barnettwilliams

This comment has been minimized.

Copy link

@barnettwilliams barnettwilliams commented May 8, 2019

Hi @pbarunsarathy could you post a snippet for date format Month-Day (ex: May 08, 2019)?

@pbaruns

This comment has been minimized.

Copy link
Owner Author

@pbaruns pbaruns commented Mar 14, 2020

Updated code to include a custom long date and time.
And moved the cursor to the end after inserting date/time.

@captpetearnow

This comment has been minimized.

Copy link

@captpetearnow captpetearnow commented Apr 27, 2020

How do you change the time format from 24 hr to 12 hr pm or am?

@captpetearnow

This comment has been minimized.

Copy link

@captpetearnow captpetearnow commented May 1, 2020

How can you get daylight savings time?

@armansadeghi1234

This comment has been minimized.

Copy link

@armansadeghi1234 armansadeghi1234 commented Feb 18, 2021

This is great. Thank you. What is the easiest way to have this menu always available when I am using a Google Doc, instead of just this one particular one?

@pbaruns

This comment has been minimized.

Copy link
Owner Author

@pbaruns pbaruns commented Feb 19, 2021

The way Google Docs work is that we cannot have it altered to have this menu available on all Docs. The code can only be made Doc-specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment