Js获取当前日期时间

时间:2023-03-08 16:47:39
Js获取当前日期时间
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( ); //获取日期与时间
//当月天数
var curMonthDays = new Date(myDate.getFullYear(), (myDate.getMonth()+1), 0).getDate();
*/
当月天数
var curMonthDays = new Date(myDate.getFullYear(), (myDate.getMonth()+1), 0).getDate();
通过JQ生成年月日下拉框数据,支持定位功能
jS部分:
var year = 2005;
var month = 12;
var day = 20;
{literal}
$(document).ready(function(){
set_date_select(year,month,day);
$(’#month’).change(function(){
var tmp_year = $(”#year”).val();
var tmp_month = $(”#month”).val();
set_date_select(tmp_year,tmp_month,0);
});
});
function set_date_select(year,month,day){
$(”#year”).empty();
$(”#month”).empty();
$(”#day”).empty();
//年
var myDate = new Date();
var year_num = myDate.getFullYear()-1900+1;
for(var i=0;i<year_num;i++){
var tmp_year = 1900+i;
if(tmp_year == year )
$(’#year’).append(”<option value=’”+tmp_year+”‘ selected=’selected’>”+tmp_year+”</option>”);
else
$(’#year’).append(”<option value=’”+tmp_year+”‘>”+tmp_year+”</option>”);
}
//月
for(var i=1;i<=12;i++){
if(i == month)
$(’#month’).append(”<option value=’”+i+”‘ selected=’selected’>”+i+”</option>”);
else
$(’#month’).append(”<option value=’”+i+”‘>”+i+”</option>”);
}
//当月天数
var curMonthDays = 31;
if(month > 0 && month <= 12){
var curMonthDays = new Date(myDate.getFullYear(), ( month ), 0).getDate();
}
//日
for(var i=1;i<=curMonthDays;i++){
if(i == day)
$(’#day’).append(”<option value=’”+i+”‘ selected=’selected’>”+i+”</option>”);
else
$(’#day’).append(”<option value=’”+i+”‘ >”+i+”</option>”);
}
}
HTML部分:
<select name=”year” id=”year”></select>
<select name=”month” id=”month”></select>
<select name=”day” id=”day”></select>