//獲得本周的開始日期
//getWeekStartDate()
//獲得本周的結束日期
//getWeekEndDate()
//獲得上周的開始日期
//getLastWeekStartDate()
//獲得上周的結束日期
//getLastWeekEndDate()
//獲得本月的開始日期
//getMonthStartDate()
//獲得本月的結束日期
//getMonthEndDate()
//獲得上月開始日期
function getLastMonthStartDate()
//獲得上月結束日期
function getLastMonthEndDate()
//獲得Q1開始日期
//getQ1StartDate()
//獲得Q1結束日期
//getQ1EndDate()
//獲得Q2開始日期
//getQ2StartDate()
//獲得Q2結束日期
//getQ2EndDate()
//獲得Q3開始日期
//getQ3StartDate()
//獲得Q3結束日期
//getQ3EndDate()
//獲得Q4開始日期
//getQ4StartDate()
//獲得Q4結束日期
//getQ4EndDate()
//獲得lastYear開始日期
//getLastYearStartDate()
//獲得lastYear結束日期
//getLastYearEndDate()
//格式化日期
//formatDate
//獲得本月的天數
//getThisMonthDays
//獲得上月的天數
//getLastMonthDays
//去除時分秒 set Date to 00:00:00
function DateToZero
//檢查日期的結束時間不能小於開始值間
//CheckDate
//檢查日期的開始和結束時間是否為同一天
//CheckTheSameDate
//以今天為基準,計算往前、往後的幾天
//getDateCount
呼叫範例
console.log(getDateCount(1));
console.log(getDateCount(-1));
const d = new Date();
console.log(CheckDate(d,d));
console.log(CheckTheSameDate(d,d));
以下程式碼,複製貼上到.js,即可呼叫各函式使用。
//20221007 日期函數
//=========================================================================================================
//獲得本周的開始日期
//getWeekStartDate(): string {
function getWeekStartDate()
{
const now = new Date(); //當前日期
const nowDayOfWeek = now.getDay(); //今天本周的第幾天 0(周日) 到 6(周六)
const nowDay = now.getDate(); //當前日
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const weekStartDate = new Date(
nowYear, nowMonth, nowDay - nowDayOfWeek
);
return this.formatDate(weekStartDate);
}
//獲得本周的結束日期
//getWeekEndDate(): string {
function getWeekEndDate()
{
const now = new Date(); //當前日期
const nowDayOfWeek = now.getDay(); //今天本周的第幾天 0(周日) 到 6(周六)
const nowDay = now.getDate(); //當前日
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const weekEndDate = new Date(
nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)
);
return this.formatDate(weekEndDate);
}
//獲得上周的開始日期
//getLastWeekStartDate(): string {
function getLastWeekStartDate()
{
const now = new Date(); //當前日期
const nowDayOfWeek = now.getDay(); //今天本周的第幾天 0(周日) 到 6(周六)
const nowDay = now.getDate(); //當前日
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const weekStartDate = new Date(
nowYear, nowMonth, nowDay - nowDayOfWeek - 7
);
return formatDate(weekStartDate);
}
//獲得上周的結束日期
//getLastWeekEndDate(): string {
function getLastWeekEndDate()
{
const now = new Date(); //當前日期
const nowDayOfWeek = now.getDay(); //今天本周的第幾天 0(周日) 到 6(周六)
const nowDay = now.getDate(); //當前日
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const weekEndDate = new Date(
nowYear, nowMonth, nowDay - nowDayOfWeek - 1
);
return this.formatDate(weekEndDate);
}
//獲得本月的開始日期
//getMonthStartDate(): string {
function getMonthStartDate(){
const now = new Date(); //當前日期
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const monthStartDate = new Date(nowYear, nowMonth, 1);
return this.formatDate(monthStartDate);
}
//獲得本月的結束日期
//getMonthEndDate(): string {
function getMonthEndDate()
{
const now = new Date(); //當前日期
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const monthEndDate = new Date(
nowYear, nowMonth,
this.getThisMonthDays(nowYear, nowMonth + 1)
);
return this.formatDate(monthEndDate);
}
//獲得上月開始日期
function getLastMonthStartDate(){
const now = new Date(); //當前日期
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const lastYear = lastMonthDate.getFullYear();
const lastMonth = lastMonthDate.getMonth();
const lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return this.formatDate(lastMonthStartDate);
}
//獲得上月結束日期
function getLastMonthEndDate(){
const now = new Date(); //當前日期
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const lastMonth = lastMonthDate.getMonth();
const lastMonthEndDate = new Date(
nowYear, lastMonth,
this.getLastMonthDays(nowYear, nowMonth)
);
return this.formatDate(lastMonthEndDate);
}
//獲得Q1開始日期
//getQ1StartDate(): string {
function getQ1StartDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q1StartDate = new Date(nowYear, 0, 1);
return this.formatDate(Q1StartDate);
}
//獲得Q1結束日期
//getQ1EndDate(): string {
function getQ1EndDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q1EndDate = new Date(nowYear, 2, 31);
return this.formatDate(Q1EndDate);
}
//獲得Q2開始日期
//getQ2StartDate(): string {
function getQ2StartDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q2StartDate = new Date(nowYear, 3, 1);
return this.formatDate(Q2StartDate);
}
//獲得Q2結束日期
//getQ2EndDate(): string {
function getQ2EndDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q2EndDate = new Date(nowYear, 5, 30);
return this.formatDate(Q2EndDate);
}
//獲得Q3開始日期
//getQ3StartDate(): string {
function getQ3StartDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q3StartDate = new Date(nowYear, 6, 1);
return this.formatDate(Q3StartDate);
}
//獲得Q3結束日期
//getQ3EndDate(): string {
function getQ3EndDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q3EndDate = new Date(nowYear, 8, 30);
return this.formatDate(Q3EndDate);
}
//獲得Q4開始日期
//getQ4StartDate(): string {
function getQ4StartDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q4StartDate = new Date(nowYear, 9, 1);
return this.formatDate(Q4StartDate);
}
//獲得Q4結束日期
//getQ4EndDate(): string {
function getQ4EndDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
const Q4EndDate = new Date(nowYear, 11, 31);
return this.formatDate(Q4EndDate);
}
//獲得lastYear開始日期
//getLastYearStartDate(): string {
function getLastYearStartDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年-1
nowYear += (nowYear < 2000) ? 1900 : 0;
const thisYearStartDate = new Date(nowYear, 0, 1);
return this.formatDate(thisYearStartDate);
}
//獲得lastYear結束日期
//getLastYearEndDate(): string {
function getLastYearEndDate()
{
const now = new Date(); //當前日期
let nowYear = now.getFullYear()-1; //當前年-1
nowYear += (nowYear < 2000) ? 1900 : 0;
const lastYearEndDate = new Date(nowYear, 11, 31);
return this.formatDate(lastYearEndDate);
}
//格式化日期
//formatDate(dateArg: any): string {
function formatDate(dateArg){
const date = new Date(dateArg);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const formatMonth = month < 10 ? `0${month}` : month;
const formatDay = day < 10 ? `0${day}` : day;
return `${year}/${formatMonth}/${formatDay}`
}
//獲得本月的天數
//getThisMonthDays(year: number, month: number): number {
function getThisMonthDays(year, month)
{
const now = new Date(); //當前日期
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const thisMonthDays = new Date(nowYear, nowMonth + 1, 0).getDate();
return thisMonthDays;
}
//獲得上月的天數
//getLastMonthDays(year: number, month: number): number {
function getLastMonthDays(year, month){
const now = new Date(); //當前日期
const nowMonth = now.getMonth(); //當前月 0(1月)到 11(12月)
let nowYear = now.getFullYear(); //當前年
nowYear += (nowYear < 2000) ? 1900 : 0;
let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
const lastMonthDays = new Date(nowYear, nowMonth, 0).getDate();
return lastMonthDays
}
//去除時分秒 set Date to 00:00:00
function DateToZero(value){
let date = new Date(value.getTime());
date.setHours(0, 0, 0, 0);
return date;
}
//檢查日期的結束時間不能小於開始值間
//CheckDate(startDate: Date, endDate: Date) {
function CheckDate(startDate, endDate){
if (this.DateToZero(endDate).getTime() >= this.DateToZero(startDate).getTime())
return true
else
return false;
}
//檢查日期的開始和結束時間是否為同一天
//CheckTheSameDate(startDate: Date, endDate: Date) {
function CheckTheSameDate(startDate, endDate){
if (this.DateToZero(endDate).getTime() === this.DateToZero(startDate).getTime())
return true
else
return false;
}
//以今天為基準,計算往前、往後的幾天
//getDateCount(addDayCount: number): string {
function getDateCount(addDayCount){
const today = new Date();
//獲取addDayCount天後的日期
today.setDate(today.getDate() + addDayCount);
const year = today.getFullYear();
const month = today.getMonth() + 1; //獲取當前月份的日期
const day = today.getDate();
const formatMonth = month < 10 ? `0${month}` : month;
const formatDay = day < 10 ? `0${day}` : day;
return `${year}-${formatMonth}-${formatDay}`
}
電子發票技術客服,電話:(02)89782365
如果有什麼問題,也可以在下方留言處與我們分享,感謝您!!
手刀試用=>簡單王電子發票 (支援串接ezPay、Amego光貿)
手刀試用=>蝦皮對帳王
手刀試用=>康捷雲端ERP
相關文章
👉 蝦皮對帳王-蝦皮對帳結果一覽表-為什麼要對帳?無對應???
👉 蝦皮對帳王-對帳原理分析-有訂單,有撥款だいじょうぶ(大丈夫);有訂單,沒撥款 歐NO~~~~~!!!

Comments