如何获取月份和年份中字母的月份和年份的第一个和最后一个日期

时间:2018-07-02 05:07:16

标签: javascript jquery

我有一个textbox,日期是 2016年3月
.now,我想要该月的第一个和最后一个日期。

3 个答案:

答案 0 :(得分:1)

在这里您可以将月份名称作为字符串传递

var year = 2018;
var sMonthName = "March"; 
var iMonthNo = new Date(sMonthName + "01, "+year).getMonth(); 
var FirstDay = new Date(year, iMonthNo, 1);
var LastDay = new Date(year, iMonthNo + 1, 0);

console.log(FirstDay);
console.log(LastDay);

更新:

这是小提琴:http://jsfiddle.net/gtKeL/82/

var date = document.getElementById("txtDate").value.split(" ");
var year = date[1];
var sMonthName = date[0]; 
var iMonthNo = new Date(sMonthName + "01, "+year).getMonth(); 
var FirstDay = new Date(year, iMonthNo, 1);
var LastDay = new Date(year, iMonthNo + 1, 0);

console.log(FirstDay);
console.log(LastDay);

答案 1 :(得分:0)

也许这段代码可以帮助您,您不需要任何库,请尝试

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

顺便说一句,有些浏览器只处理两位数的年份

答案 2 :(得分:0)

http://www.encodedna.com/javascript/first-and-last-day-of-a-given-month-in-javascript.htm

<!DOCTYPE html>
<html>
<head>
    <title>Get The First and Last Day of a Given Month</title>
</head>

<body>
   <body>
<input type="date" id="dt" onchange="mydate1();" onmouseout="getTheDays()" hidden/>
<input type="text" id="ndt"  onclick="mydate();" onmouseout="getTheDays()" hidden />
<input type="button" Value="Date" onclick="mydate();" onmouseout="getTheDays()" />

    <p>First Day of the Month: <label id="fday"></label></p>
    <p>Last Day of the Month: <label id="lday"></label></p>
</body>

<script>
  
  function mydate()
{
  //alert("");
document.getElementById("dt").hidden=false;
document.getElementById("ndt").hidden=true;
}
function mydate1()
{
 d=new Date(document.getElementById("dt").value);
dt=d.getDate();
mn=d.getMonth();
mn++;
yy=d.getFullYear();
document.getElementById("ndt").value=mn+"/"+yy
document.getElementById("ndt").hidden=false;
document.getElementById("dt").hidden=true;
}
    function getTheDays() {

        // THE DATE OBJECT.

        var dt = new Date(document.getElementById('dt').value );

        // GET THE MONTH AND YEAR OF THE SELECTED DATE.
        var month = dt.getMonth(),
            year = dt.getFullYear();

        // GET THE FIRST AND LAST DATE OF THE MONTH.
        var FirstDay = new Date(year, month, 1);
        var LastDay = new Date(year, month + 1, 0);

        // FINALLY, GET THE DAY.
        var weekday = new Array();
        weekday[0] = "Sunday";
        weekday[1] = "Monday";
        weekday[2] = "Tuesday";
        weekday[3] = "Wednesday";
        weekday[4] = "Thursday";
        weekday[5] = "Friday";
        weekday[6] = "Saturday";

        if (typeof weekday[FirstDay.getDay()] != 'undefined') {     // CHECK FOR 'undefined'.
            document.getElementById('fday').innerHTML = weekday[FirstDay.getDay()] +
                ' (' + FirstDay.toDateString('dd/mon/yyyy') + ')';
            document.getElementById('lday').innerHTML = weekday[LastDay.getDay()] +
                ' (' + LastDay.toDateString('dd/mon/yyyy') + ')'; ;
        }
        else {
            document.getElementById('fday').innerHTML = '';
            document.getElementById('lday').innerHTML = '';
        }
    }
</script>
</html>