天天看點

JS擷取目前日期和時間的方法,并按照YYYY-MM-DD格式化

閑暇之餘、好東西做個收藏、以備不時之需。

轉載自  安然部落格

js擷取目前日期時間及其它操作

var mydate = new date();

mydate.getyear();        //擷取目前年份(2位)

mydate.getfullyear();    //擷取完整的年份(4位,1970-????)

mydate.getmonth();       //擷取目前月份(0-11,0代表1月)

mydate.getdate();        //擷取目前日(1-31)

mydate.getday();         //擷取目前星期x(0-6,0代表星期天)

mydate.gettime();        //擷取目前時間(從1970.1.1開始的毫秒數)

mydate.gethours();       //擷取目前小時數(0-23)

mydate.getminutes();     //擷取目前分鐘數(0-59)

mydate.getseconds();     //擷取目前秒數(0-59)

mydate.getmilliseconds();    //擷取目前毫秒數(0-999)

mydate.tolocaledatestring();     //擷取目前日期

var mytime=mydate.tolocaletimestring();     //擷取目前時間

mydate.tolocalestring( );        //擷取日期與時間

/**

function getnowformatdate() {

    var date = new date();

    var seperator1 = "-";

    var seperator2 = ":";

    var year = date.getfullyear();

    var month = date.getmonth() + 1;

    var strdate = date.getdate();

    if (month >= 1 && month <= 9) {

        month = "0" + month;

    }

    if (strdate >= 0 && strdate <= 9) {

        strdate = "0" + strdate;

    var currentdate = year + seperator1 + month + seperator1 + strdate

            + " " + date.gethours() + seperator2 + date.getminutes()

            + seperator2 + date.getseconds();

    return currentdate;

}

繼續閱讀