[tools] javascript date processing collection

javascript date processing collection

let time = new Date()
console.log(time) // Wed Oct 23 2019 10:47:04 GMT+0800 (China standard time)
console.log(time.getDate()) // 23
console.log(time.getDay()) // 3 = > Wednesday
console.log(time.getMonth()) // 9 = = > 9 + 1 is needed to obtain this month
console.log(time.getFullYear()) // 2019
console.log(time.getTime()) // 1571799028703

Format date XXXX-MM-DD-HH-MM-SS

// Format date XXXX-MM-DD-HH-MM-SS
function formatDate(time) {
  if (!time) return ''
  let date = new Date(time)
  return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() +
    ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
}

Get the start date and end date of this week, this quarter, this month and last month

/**
 * Get the start date and end date of this week, this quarter, this month and last month
 */
var now = new Date(); //current date
var nowDayOfWeek = now.getDay(); //What day of the week is today
var nowDay = now.getDate(); //Current day
var nowMonth = now.getMonth(); //Current month
var nowYear = now.getYear(); //Current year
nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //Last month date
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();

Format date: yyyy MM DD

//Format date: yyyy MM DD
function formatDate(date) {
  var myyear = date.getFullYear();
  var mymonth = date.getMonth() + 1;
  var myweekday = date.getDate();
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return (myyear + "-" + mymonth + "-" + myweekday);
}

Get date after n months

// Get date after n months
function getDateAfterNMonth(n) {
  let time = new Date()
  time.setMonth(time.getMonth() + n)
  return time
}
let DayBeforeSixMonths = getDateAfterNMonth(-6)

Get the morning time of the day

// Get the morning time of the day
function getFirstTimeOfDay(date) {
  return new Date(date.setHours(0, 0, 0, 0))
}
let firstTime = getFirstTimeOfDay(DayBeforeSixMonths)
console.log(firstTime) // Tue Apr 23 2019 00:00:00 GMT+0800 (China standard time)
console.log(formatDate(firstTime)) // 2019-4-23 0:0:0

Get the time of the day at 23:59:59

// Get the time of the day at 23:59:59
function getLastTimeOfDay(date) {
  return new Date(new Date(date.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
}
let lastTime = getLastTimeOfDay(DayBeforeSixMonths)
console.log(lastTime) // Tue Apr 23 2019 23:59:59 GMT+0800 (China standard time)
console.log(formatDate(lastTime)) // 2019-4-23 23:59:59
console.log(lastTime.toLocaleString()) // 11:59:59 pm on April 23, 2019

Get Monday of the week

//Get Monday of the week
function getFirstDayOfWeek(date) {
  let weekday = date.getDay() || 7 //Gets the day of the week, and getDay() returns an integer between 0 (Sunday) and 6 (Saturday). 0|7 is 7, that is, the value of weekday is 1-7
  date.setDate(date.getDate() - weekday + 1) //weekday-1 days ahead, the year and month will change automatically
  return this.formatDate(date)
}

Get days of a month

//Get days of a month
function getMonthDays(myMonth) {
  var monthStartDate = new Date(nowYear, myMonth, 1);
  var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  return days;
}

Get the start month of the quarter

//Get the start month of the quarter
function getQuarterStartMonth() {
  var quarterStartMonth = 0;
  if (nowMonth < 3) {
    quarterStartMonth = 0;
  }
  if (2 < nowMonth && nowMonth < 6) {
    quarterStartMonth = 3;
  }
  if (5 < nowMonth && nowMonth < 9) {
    quarterStartMonth = 6;
  }
  if (nowMonth > 8) {
    quarterStartMonth = 9;
  }
  return quarterStartMonth;
}

Get the start date of the week

//Get the start date of the week
function getWeekStartDate() {
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  return formatDate(weekStartDate);
}

Get the end date of the week

//Get the end date of the week
function getWeekEndDate() {
  var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  return formatDate(weekEndDate);
}

Get start date of last week

//Get start date of last week
function getLastWeekStartDate() {
  var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
  return formatDate(weekStartDate);
}

Get last week's end date

//Get last week's end date
function getLastWeekEndDate() {
  var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
  return formatDate(weekEndDate);
}

Get the start date of the month

//Get the start date of the month
function getMonthStartDate() {
  var monthStartDate = new Date(nowYear, nowMonth, 1);
  return formatDate(monthStartDate);
}

Get the end date of the month

//Get the end date of the month
function getMonthEndDate() {
  var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  return formatDate(monthEndDate);
}

Get start date of last month

//Get start date of last month
function getLastMonthStartDate() {
  var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  return formatDate(lastMonthStartDate);
}

Get last month end date

//Get last month end date
function getLastMonthEndDate() {
  var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  return formatDate(lastMonthEndDate);
}

Get the start date of the quarter

//Get the start date of the quarter
function getQuarterStartDate() {
  var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  return formatDate(quarterStartDate);
}

Get the end date of the quarter

//Get the end date of the quarter
function getQuarterEndDate() {
  var quarterEndMonth = getQuarterStartMonth() + 2;
  var quarterStartDate = new Date(nowYear, quarterEndMonth,
    getMonthDays(quarterEndMonth));
  return formatDate(quarterStartDate);
}

Keywords: Javascript

Added by incubi on Mon, 04 Nov 2019 17:49:00 +0200