首页 > 代码库 > time

time

///$.twoDates("startdate",‘enddate‘,3);
$.extend({
    setTwoDates: function (startinputid, endinputid, difference) {

        var year = 0;
        var today = "";
        var mydate = new Date();
        var mymonth = mydate.getMonth() + 1;
        var myday = mydate.getDate();
        var myyear = mydate.getFullYear();
        year = (myyear > 200) ? myyear : 1900 + myyear;
        if (mymonth >= 10) { mymonth = mymonth; } else { mymonth = "0" + mymonth; }
        if (myday >= 10) { myday = myday; } else { myday = "0" + myday; }
        today = year + ‘-‘ + mymonth + ‘-‘ + myday;

        $("#" + endinputid).val(today);

        var enddate = mydate.valueOf();
        enddate = enddate - difference * 24 * 60 * 60 * 1000;
        mydate = new Date(enddate);

        var year = 0;
        var today = "";
        var mymonth = mydate.getMonth() + 1;
        var myday = mydate.getDate();
        var myyear = mydate.getFullYear();
        year = (myyear > 200) ? myyear : 1900 + myyear;
        if (mymonth >= 10) { mymonth = mymonth; } else { mymonth = "0" + mymonth; }
        if (myday >= 10) { myday = myday; } else { myday = "0" + myday; }
        today = year + ‘-‘ + mymonth + ‘-‘ + myday;

        $("#" + startinputid).val(today);


    }
})

///$("#elementid").setDefaultDate();

$.fn.extend({
    setDefaultDate: function (datetype) {
        var day = 0;
        var month = 0;
        var year = 0;
        var today = "";
        mydate = new Date();
        mydate.setTime(mydate.getTime() - 24 * 60 * 60 * 1000);
        mymonth = mydate.getMonth()+1;
        myday = mydate.getDate();
        myyear = mydate.getFullYear();
        year = (myyear > 200) ? myyear : 1900 + myyear;
        if (mymonth >= 10) { mymonth = mymonth; } else { mymonth = "0" + mymonth; }
        if (myday >= 10) { myday = myday; } else { myday = "0" + myday; }
        switch (datetype)
        {
            case "day":
                today = year + ‘-‘ + mymonth + ‘-‘ + myday;
                break;
            case "month":
                today = year + ‘-‘ + mymonth;
                break;
            case "year":
                today = year;
            default:
                break;
        }

        $(this).val(today);
    }
})

 

time