时间计算

来源:互联网 发布:mysql 昨天0点到24点 编辑:程序博客网 时间:2024/05/01 08:56

时间的阶段性

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jsGetDate.aspx.cs" Inherits="bindData.connection.jsGetDate" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>取得日期</title>    <script type="text/javascript" language="javascript">        function setCurTime(oid) {            var now = oid;            var year = now.getFullYear();            var month = now.getMonth() + 1;            var day = now.getDate();            var hours = now.getHours();            var minutes = now.getMinutes();            var seconds = now.getSeconds();            month = month < 10 ? "0" + month : month;            day = day < 10 ? "0" + day : day;            hours = hours < 10 ? "0" + hours : hours;            minutes = minutes < 10 ? "0" + minutes : minutes;            seconds = seconds < 10 ? "0" + seconds : seconds;            var time = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;            return time;        }        var nowDate = new Date();        function getToday() {            var year = nowDate.getYear();            var month = Number(nowDate.getMonth()) + 1;            var day = nowDate.getDate();            document.getElementById("text_date").value = year + "-" + month + "-" + day;        }        function getMonth() {            var month = nowDate.getMonth() + 1;            month = (month < 10) ? '-0' + month : '-' + month;            var currentMonth = nowDate.getYear() + month;            document.getElementById("text_date").value = currentMonth;        }        function firstDayByThisWeek() {            var weekFirstDay = new Date(nowDate - (nowDate.getDay() - 1) * 86400000);            document.getElementById("text_date").value = setCurTime(weekFirstDay);        }        function endDayByThisWeek() {            var weekFirstDay = new Date(nowDate - (nowDate.getDay() - 1) * 86400000);            var weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000);            document.getElementById("text_date").value = setCurTime(weekLastDay);        }        function firstDayByThisMonth() {            var monthFirstDay = new Date(nowDate.getYear(), nowDate.getMonth(), 1);            document.getElementById("text_date").value = setCurTime(monthFirstDay);        }        function endDayByThisMonth() {            var monthFirstDay = new Date(nowDate.getYear(), nowDate.getMonth(), 1);            var monthEndDay = new Date(monthFirstDay - 86400000);            document.getElementById("text_date").value = setCurTime(monthEndDay);        }        function firstDayByThisQuarter() {            var quarterFirstDay = "";            if (nowDate.getMonth() < 3) {                quarterFirstDay = new Date(nowDate.getYear(), 0, 1);            }            else if (nowDate.getMonth() > 2 && nowDate.getMonth() < 6) {                quarterFirstDay = new Date(nowDate.getYear(), 3, 1);            }            else if (nowDate.getMonth() > 5 && nowDate.getMonth() < 9) {                quarterFirstDay = new Date(nowDate.getYear(), 6, 1);            }            else if (nowDate.getMonth() > 8) {                quarterFirstDay = new Date(nowDate.getYear(), 9, 1);            }            document.getElementById("text_date").value = setCurTime(quarterFirstDay);        }        function endDayByThisQuarter() {            var quarterEndDay = "";            if (nowDate.getMonth() < 3) {                quarterEndDay = new Date(nowDate.getYear(), 2, 31);            }            else if (nowDate.getMonth() > 2 && nowDate.getMonth() < 6) {                quarterEndDay = new Date(nowDate.getYear(), 5, 30);            }            else if (nowDate.getMonth() > 5 && nowDate.getMonth() < 9) {                quarterEndDay = new Date(nowDate.getYear(), 8, 30);            }            else if (nowDate.getMonth() > 8) {                quarterEndDay = new Date(nowDate.getYear(), 11, 31);            }            document.getElementById("text_date").value = setCurTime(quarterEndDay);        }        function firstMonthByThisQuarter() {            var quarterFirstMonth = "";            if (nowDate.getMonth() < 3) {                quarterFirstMonth = nowDate.getYear() + '-01';            }            else if (nowDate.getMonth() > 2 && nowDate.getMonth() < 6) {                quarterFirstMonth = nowDate.getYear() + '-04';            }            else if (nowDate.getMonth() > 5 && nowDate.getMonth() < 9) {                quarterFirstMonth = nowDate.getYear() + '-07';            }            else if (nowDate.getMonth() > 8) {                quarterFirstMonth = nowDate.getYear() + '-10';            }            document.getElementById("text_date").value = quarterFirstMonth;        }        function endMonthByThisQuarter() {            var quarterEndMonth = "";            if (nowDate.getMonth() < 3) {                quarterEndMonth = nowDate.getYear() + '-03';            }            else if (nowDate.getMonth() > 2 && nowDate.getMonth() < 6) {                quarterEndMonth = nowDate.getYear() + '-06';            }            else if (nowDate.getMonth() > 5 && nowDate.getMonth() < 9) {                quarterEndMonth = nowDate.getYear() + '-09';            }            else if (nowDate.getMonth() > 8) {                quarterEndMonth = nowDate.getYear() + '-12';            }            document.getElementById("text_date").value = quarterEndMonth;        }        function firstDayByThisYear() {            var yearFirstDay = new Date(nowDate.getYear(), 0, 1);            document.getElementById("text_date").value = setCurTime(yearFirstDay);        }        function endDayByThisYear() {            var yearLastDay = new Date(nowDate.getYear(), 11, 31);            document.getElementById("text_date").value = setCurTime(yearLastDay);        }        function firstMonthByThisYear() {            var yearFirstMonth = nowDate.getYear() + '-01';            document.getElementById("text_date").value = yearFirstMonth;        }        function endMonthByThisYear() {            var yearEndMonth = nowDate.getYear() + '-12';            document.getElementById("text_date").value = yearEndMonth;        }    </script></head><body>    <form id="form1" runat="server">    <div>        <input type="text" id="text_date" />        <br />        <input type="button" value="今天" onclick="getToday();" />        <br />        <input type="button" value="当前月" onclick="getMonth();" />        <br />        <input type="button" value="本周第一天" onclick="firstDayByThisWeek();" />        <input type="button" value="本周最后一天" onclick="endDayByThisWeek();" />        <br />        <input type="button" value="本月第一天" onclick="firstDayByThisMonth();" />        <input type="button" value="本月最后一天" onclick="endDayByThisMonth();" />        <br />        <input type="button" value="本季第一天" onclick="firstDayByThisQuarter();" />        <input type="button" value="本季最后一天" onclick="endDayByThisQuarter();" />        <br />        <input type="button" value="本季开始年月" onclick="firstMonthByThisQuarter();" />        <input type="button" value="本季最后年月" onclick="endMonthByThisQuarter();" />        <br />        <input type="button" value="本年第一天" onclick="firstDayByThisYear();" />        <input type="button" value="本年最后一天" onclick="endDayByThisYear();" />        <br />        <input type="button" value="本年开始年月" onclick="firstMonthByThisYear();" />        <input type="button" value="本年最后年月" onclick="endMonthByThisYear();" />    </div>    </form></body></html>


 

两个时间的比较

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="bindData.connection.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script type="text/javascript" language="javascript">        function daysBetween(DateOne, DateTwo) {            var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'));            var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1);            var OneYear = DateOne.substring(0, DateOne.indexOf('-'));            var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'));            var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1);            var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));            var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000);            return cha;        }        function aa() {            if (daysBetween("2009-11-09 12:22:24", "2009-11-09 12:22:23") <= 0) {                alert("时间一要大于时间二!")                return false;            } else {                alert("ok!")            }        }    </script></head><body>    <form id="form1" runat="server">    <div>    <input type="button"  onclick="aa();" />;    </div>    </form></body></html>


 

 

 

原创粉丝点击