var date = new Date();
var year = dategetFullYear();//获取当前年份
var month = dategetMonth()+1;//获取当前月,因为getMonth()的值为0-11,所以要+1
var days = dategetDate();//获取当前日期
var day = dategetDay();//获取当前星期 返回0-6 对应 星期日—星期六
var hour = dategetHours();//获取当前小时
var mins = dategetMinutes();//获取当前分钟
var secs = dategetSeconds();//获取当前秒数
var time = dategetTime();//以毫秒表示的日期
1,Date date = new Date();
Systemoutprintln(datetoLocaleString());
2,以上程序是使用上述格式打印出当前的日期。
3,以下是根据上述格式的字符串构造出一个date对象供程序使用
public static Date constructDate(String time){
boolean result = Patternmatches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", time);
if(!result)
return null;
String ymd = timesplit(" ")[0];
String hms = timesplit(" ")[1];
String[] ymds = ymdsplit("-");
String[] hmss = hmssplit(":");
return new Date(IntegerparseInt(ymds[0])-1900,IntegerparseInt(ymds[1])-1,IntegerparseInt(ymds[2]),
IntegerparseInt(hmss[0]),IntegerparseInt(hmss[1]),IntegerparseInt(hmss[2]));
}
<html>
<head>
<script language="JavaScript">
var a,b,c;
var dateTime = new Date();
var fixTime = new Date();
fixTimesetHours(10);
fixTimesetMinutes(30);
fixTimesetSeconds(0);
var difTime=dateTimegetTime()>fixTimegetTime()dateTimegetTime()-fixTimegetTime():fixTimegetTime()-dateTimegetTime();
var t=toTime(difTime);
alert(t);
function toTime(milliSecond){
var h=parseInt(milliSecond/(60601000));
var temp=milliSecond%(60601000);
var m=parseInt(temp/(601000));
var s=parseInt((temp%(601000))/1000);
return (h<10"0"+h:h)+":"+(m<10"0"+m:m)+":"+(s<10"0"+s:s);
}
</script>
</head>
<body>
</body>
</html>
你看看,我测试是没有问题的e/
<html>
<script type="text/javascript">
windowonload = function()
{
var show = documentgetElementById("show");
setInterval(function()
{
var time = new Date();
//程序计时的月从0开始取值后+1
var m = timegetMonth() + 1;
var t = timegetFullYear() + "-" + m + "-" + timegetDate() + " " + timegetHours() + ":" + timegetMinutes() + ":" + timegetSeconds();
showinnerHTML = t;
}, 1000);
};
</script>
<div id="show"></div>
</html>
如果你在
java中会获得数据库时间就行了,在jsp中写在<%%>中的语句就是纯java代码了,在这个里面获得时间比如说String
time=;
然后再js中:
var
dbTime=<%=time%>;
然后就可以用dbTime做要做的事情了。
以上就是关于js获得当前日期和时间的代码是什么全部的内容,包括:js获得当前日期和时间的代码是什么、JS 取得当前日期格式为 xxxx-xx-xx、js将string类型转为时间类型:日期不需要,只要时分秒,限制每天的10点30分来减去当前的时间。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)