SoFunction
Updated on 2025-02-28

js get date: yesterday today and tomorrow, the day after tomorrow

js date Get the first function today, yesterday, and tomorrow

  function getDay(day){
    var today = new Date()
    // Get the timestamp (millisecond level)    /*
       Day is 1, then tomorrow's time stamp
       Day is -1, which is yesterday's time stamp
       Day is -2, then the time stamp of the day before yesterday
     */
    var targetday_milliseconds = () + 1000 * 60 * 60 * 24 * day
    // (Timestamp): Set the time of the current date    (targetday_milliseconds)
    ('today=', today) // today= Sun Mar 05 2023 16:14:56 GMT+0800 (China Standard Time)    var tYear = () // Year    var tMonth = () // moon    var tDate = () // day    tMonth = (tMonth + 1)
    tDate = (tDate)
    ('Return to year, month, day=', tYear + '-' + tMonth + '-' + tDate)
    return tYear + '-' + tMonth + '-' + tDate
  }
  function doHandleMonth(month) {
    var m = month
    if (().length == 1) {
      m = '0' + month
    }
    return m
  }

js get the second function of today, yesterday, tomorrow's date

/*
   * @params date
   * @params type date prev/current/next Yesterday/Today/Tomorrow
   * @params fmt date splicing symbol
 */
function getDays(date, type, fmt) {
    let currentDate = new Date(date)
    let y = ()
    let m = () + 1
    let d = ()
    function dateFormat(date, fmt) {
        let y = new Date(date).getFullYear()
        let m = new Date(date).getMonth() + 1
        let d = new Date(date).getDate()
        return `${y}${fmt}${m}${fmt}${d}`
    }
    switch (type) {
        case "prev":
            if (d - 1 < 1) {
                if (m - 1 < 1) {
                    y = y - 1
                    m = 12
                } else {
                    m = m - 1
                }
                d = new Date(y, m, 0).getDate()
            } else {
                d = d - 1
            }
            break
        case "current":
            break
        case "next":
            if (d + 1 > new Date(y, m, 0).getDate()) {
                if (m + 1 > 12) {
                    y = y + 1
                    m = 1
                    d = 1
                } else {
                    m = m + 1
                    d = 1
                }
            } else {
                d = d + 1
            }
            break;
    default:
      break;
    }
    return dateFormat(new Date(`${y}-${m}-${d}`), fmt)
}
(getDays(new Date('2023-5-13'), "prev", "-"));
(getDays(new Date('2023-5-30'), "next", "-"));
(getDays(new Date('2023-5-31'), "next", "-"));

Replenish

<html>
<head>
<meta http-equiv="Content-Type" content="textml; charset=utf-8">
<title>jsGet date:The day before yesterday、yesterday、today、tomorrow、the day after tomorrow - </title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function GetDateStr(AddDayCount) {
var dd = new Date();
(()+AddDayCount);//Get the date of AddDayCount divavar y = ();
var m = ()+1;//Get the date of the current monthvar d = ();
return y+"-"+m+"-"+d;
}
("The day before yesterday:"+GetDateStr(-2));
("<br />Yesterday:"+GetDateStr(-1));
("<br />Today:"+GetDateStr(0));
("<br />Tomorrow:"+GetDateStr(1));
("<br />The day after tomorrow:"+GetDateStr(2));
("<br />The day after tomorrow:"+GetDateStr(3));
&lt;/script&gt;
&lt;/body&gt;
&lt;html&gt;

One of the methods is: (dateVal). This function is powerful, but it has a fatal disadvantage, that is, it does not support the commonly used "year-month-day" format. Short dates can be used with "/" or "-" as date separators, but must be represented in the format of month/day/year, such as "7/20/96".

Another way is to use split, such as:

var dtStr = "2023-05-25";
var dtArr = ("-");
var dt = new Date(dtArr[0], dtArr[1], dtArr[2]);

However, this method is relatively rigid and requires a fixed date format and can only be used if there is no way.
If we can separate the year, month and day, try to separate them, for example, ASP outputs year, month and day respectively. Then use new Date to process, and the returned date type.

Date formatting

&lt;script language="javascript" type="text/javascript"&gt;&lt;!--
/**
 * Extension of Date, convert Date into String in the specified format
 * Month (M), day (d), 12 hours (h), 24 hours (H), minute (m), seconds (s), week (E), quarter (q) 1-2 placeholders can be used
 * Year (y) can be used with 1-4 placeholders, millisecond (S) can only be used with 1 placeholder (it is a 1-3 digit number)
 * eg:
 * (new Date()).pattern("yyyy-MM-dd hh:mm:") ==> 2006-07-02 08:09:04.423
 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 2 20:09:04
 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
 * (new Date()).pattern("yyyy-M-d h:m:") ==> 2006-7-2 8:9:4.18
 */
=function(fmt) {
var o = {
"M+" : ()+1, //month"d+" : (), //day"h+" : ()%12 == 0 ? 12 : ()%12, //Hour"H+" : (), //Hour"m+" : (), //point"s+" : (), //Second"q+" : ((()+3)/3), //Quarterly"S" : () //millisecond};
var week = {
"0" : "\u65e5",
"1" : "\u4e00",
"2" : "\u4e8c",
"3" : "\u4e09",
"4" : "\u56db",
"5" : "\u4e94",
"6" : "\u516d"
};
if(/(y+)/.test(fmt)){
fmt=(RegExp.$1, (()+"").substr(4 - RegExp.$));
}
if(/(E+)/.test(fmt)){
fmt=(RegExp.$1, ((RegExp.$&gt;1) ? (RegExp.$&gt;2 ? "\u661f\u671f" : "\u5468") : "")+week[()+""]);
}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
fmt = (RegExp.$1, (RegExp.$==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
var date = new Date();
(("yyyy-MM-dd hh:mm:ss"));
// --&gt;&lt;/script&gt;

Here is the article about js acquisition date: This is all about the articles yesterday, today, tomorrow, and the day after tomorrow. For more related js acquisition date content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!