JS 獲取當前年份,月份

2022-05-07 12:21:11 字數 1130 閱讀 9923

function dohandledate()

return tyear +'-'+ m;

}function dohandleyear(tyear)

function dohandlemonth()

return m;

}//獲取完整的日期

var date=new date;

var year=date.getfullyear(); 

var month=date.getmonth()+1;

month =(month<10 ? "0"+month:month); 

var mydate = (year.tostring()+month.tostring());

注意,year.tostring()+month.tostring()不能寫成year+month。不然如果月份大於等於10,則月份為數字,會和年份相加,如201210,則會變為2022,需要加.tostring()

以下是搜到的有用內容:

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( ); //獲取日期與時間

js獲取當前時間是年份或月份

1 js獲取兩個日期之間相差的天數 function getdaysbetween datestring1,datestring2 2 js獲取當前時間是年份或月份 var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完...

如何使用PHP獲取當前年份?

如果您的伺服器支援短標籤,或者您使用php 5.4,則可以使用 隨著php朝著更加物件導向的方向發展,我很驚訝這裡沒有人引用內建的datetime類 now new datetime year now format y 或者在例項化時使用類成員訪問的單行 php 5.4 year new datet...

mysql 查詢當前年份

1.本年份 select date format now y 2.本月份 顯示數字 select date format now m 3.本月份 顯示英文 select date format now m 4.本日期 顯示數字 select date format now d 5.本日期 顯示數字和...