Skip to content

Instantly share code, notes, and snippets.

@yukihirai0505
Last active February 27, 2016 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukihirai0505/5d23464ba18213628ead to your computer and use it in GitHub Desktop.
Save yukihirai0505/5d23464ba18213628ead to your computer and use it in GitHub Desktop.
日付を扱う便利スクリプト
/**
指定日を文字列で取得する
@param nDaysAgo 何日前か
@return date
**/
function getLastNdays(nDaysAgo) {
var today = new Date();
var before = new Date();
before.setDate(today.getDate() - nDaysAgo);
return DateFormat(before);
}
/**
指定月の初日を文字列で取得する
@param nMonthAgo 何カ月前か
@return date
**/
function getFirstDayOfMonth(date) {
date.setDate(1);
return DateFormat(date);
}
/**
指定月の最終日を文字列で取得する
@param nMonthAgo 何カ月前か
@return date
**/
function getLastDayOfMonth(date) {
date.setDate(1);
date.setMonth(date.getMonth() + 1);
date.setDate(0);
return DateFormat(date);
}
/**
指定月を取得する
@param nMonthAgo 何カ月前か
@return date
**/
function getLastNMonth(nMonthsAgo) {
var today = new Date();
var before = new Date();
before.setMonth(today.getMonth() - nMonthsAgo);
return before;
}
/**
日付けのフォーマット
@param date
@return date
**/
function DateFormat(date) {
var formatType = 'yyyy-MM-dd';
return Utilities.formatDate(date, 'JST', formatType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment