js怎么获得数据库里的时间
var myDate = new Date();
myDategetYear(); //获取当前年份(2位)
myDategetFullYear(); //获取完整的年份(4位,1970-)
myDategetMonth(); //获取当前月份(0-11,0代表1月)
myDategetDate(); //获取当前日(1-31)
myDategetDay(); //获取当前星期X(0-6,0代表星期天)
myDategetTime(); //获取当前时间(从197011开始的毫秒数)
myDategetHours(); //获取当前小时数(0-23)
myDategetMinutes(); //获取当前分钟数(0-59)
myDategetSeconds(); //获取当前秒数(0-59)
myDategetMilliseconds(); //获取当前毫秒数(0-999)
myDatetoLocaleDateString(); //获取当前日期
var mytime=myDatetoLocaleTimeString(); //获取当前时间
myDatetoLocaleString( ); //获取日期与时间
日期时间脚本库方法列表
DateprototypeisLeapYear 判断闰年
DateprototypeFormat 日期格式化
DateprototypeDateAdd 日期计算
DateprototypeDateDiff 比较日期差
DateprototypetoString 日期转字符串
DateprototypetoArray 日期分割为数组
DateprototypeDatePart 取日期的部分信息
DateprototypeMaxDayOfDate 取日期所在月的最大天数
DateprototypeWeekNumOfYear 判断日期所在年的第几周
StringToDate 字符串转日期型
IsValidDate 验证日期有效性
CheckDateTime 完整日期时间检查
daysBetween 日期天数差
js代码:
//---------------------------------------------------
// 判断闰年
//---------------------------------------------------
DateprototypeisLeapYear = function()
{
return (0==thisgetYear()%4&&((thisgetYear()%100!=0)||(thisgetYear()%400==0)));
}
//---------------------------------------------------
// 日期格式化
// 格式 YYYY/yyyy/YY/yy 表示年份
// MM/M 月份
// W/w 星期
// dd/DD/d/D 日期
// hh/HH/h/H 时间
// mm/m 分钟
// ss/SS/s/S 秒
//---------------------------------------------------
DateprototypeFormat = function(formatStr)
{
var str = formatStr;
var Week = ['日','一','二','三','四','五','六'];
str=strreplace(/yyyy|YYYY/,thisgetFullYear());
str=strreplace(/yy|YY/,(thisgetYear() % 100)>9(thisgetYear() % 100)toString():'0' + (thisgetYear() % 100));
str=strreplace(/MM/,thisgetMonth()>9thisgetMonth()toString():'0' + thisgetMonth());
str=strreplace(/M/g,thisgetMonth());
str=strreplace(/w|W/g,Week[thisgetDay()]);
str=strreplace(/dd|DD/,thisgetDate()>9thisgetDate()toString():'0' + thisgetDate());
str=strreplace(/d|D/g,thisgetDate());
str=strreplace(/hh|HH/,thisgetHours()>9thisgetHours()toString():'0' + thisgetHours());
str=strreplace(/h|H/g,thisgetHours());
str=strreplace(/mm/,thisgetMinutes()>9thisgetMinutes()toString():'0' + thisgetMinutes());
str=strreplace(/m/g,thisgetMinutes());
str=strreplace(/ss|SS/,thisgetSeconds()>9thisgetSeconds()toString():'0' + thisgetSeconds());
str=strreplace(/s|S/g,thisgetSeconds());
return str;
}
//+---------------------------------------------------
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
//+---------------------------------------------------
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOnesubstring(5,DateOnelastIndexOf ('-'));
var OneDay = DateOnesubstring(DateOnelength,DateOnelastIndexOf ('-')+1);
var OneYear = DateOnesubstring(0,DateOneindexOf ('-'));
var TwoMonth = DateTwosubstring(5,DateTwolastIndexOf ('-'));
var TwoDay = DateTwosubstring(DateTwolength,DateTwolastIndexOf ('-')+1);
var TwoYear = DateTwosubstring(0,DateTwoindexOf ('-'));
var cha=((Dateparse(OneMonth+'/'+OneDay+'/'+OneYear)- Dateparse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
return Mathabs(cha);
}
//+---------------------------------------------------
//| 日期计算
//+---------------------------------------------------
DateprototypeDateAdd = function(strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date(Dateparse(dtTmp) + (1000 Number));
case 'n' :return new Date(Dateparse(dtTmp) + (60000 Number));
case 'h' :return new Date(Dateparse(dtTmp) + (3600000 Number));
case 'd' :return new Date(Dateparse(dtTmp) + (86400000 Number));
case 'w' :return new Date(Dateparse(dtTmp) + ((86400000 7) Number));
case 'q' :return new Date(dtTmpgetFullYear(), (dtTmpgetMonth()) +
Number3, dtTmpgetDate(), dtTmpgetHours(), dtTmpgetMinutes(),
dtTmpgetSeconds());
case 'm' :return new Date(dtTmpgetFullYear(), (dtTmpgetMonth()) +
Number, dtTmpgetDate(), dtTmpgetHours(), dtTmpgetMinutes(),
dtTmpgetSeconds());
case 'y' :return new Date((dtTmpgetFullYear() + Number),
dtTmpgetMonth(), dtTmpgetDate(), dtTmpgetHours(), dtTmpgetMinutes(),
dtTmpgetSeconds());
}
}
//+---------------------------------------------------
//| 比较日期差 dtEnd 格式为日期型或者有效日期格式字符串
//+---------------------------------------------------
DateprototypeDateDiff = function(strInterval, dtEnd) {
var dtStart = this;
if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
{
dtEnd = StringToDate(dtEnd);
}
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000);
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 7));
case 'm' :return (dtEndgetMonth()+1)+((dtEndgetFullYear()-dtStartgetFullYear())12) - (dtStartgetMonth()+1);
case 'y' :return dtEndgetFullYear() - dtStartgetFullYear();
}
}
//+---------------------------------------------------
//| 日期输出字符串,重载了系统的toString方法
//+---------------------------------------------------
DateprototypetoString = function(showWeek)
{
var myDate= this;
var str = myDatetoLocaleDateString();
if (showWeek)
{
var Week = ['日','一','二','三','四','五','六'];
str += ' 星期' + Week[myDategetDay()];
}
return str;
}
//+---------------------------------------------------
//| 日期合法性验证
//| 格式为:YYYY-MM-DD或YYYY/MM/DD
//+---------------------------------------------------
function IsValidDate(DateStr)
{
var sDate=DateStrreplace(/(^\s+|\s+$)/g,''); //去两边空格;
if(sDate=='') return true;
//如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
//数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
var s = sDatereplace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
{
var t=new Date(sDatereplace(/\-/g,'/'));
var ar = sDatesplit(/[-/:]/);
if(ar[0] != tgetYear() || ar[1] != tgetMonth()+1 || ar[2] != tgetDate())
{
//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
return false;
}
}
else
{
//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
return false;
}
return true;
}
//+---------------------------------------------------
//| 日期时间检查
//| 格式为:YYYY-MM-DD HH:MM:SS
//+---------------------------------------------------
function CheckDateTime(str)
{
var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
var r = strmatch(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
if(dgetFullYear()!=r[1])return false;
if(dgetMonth()!=r[2])return false;
if(dgetDate()!=r[3])return false;
if(dgetHours()!=r[4])return false;
if(dgetMinutes()!=r[5])return false;
if(dgetSeconds()!=r[6])return false;
return true;
}
//+---------------------------------------------------
//| 把日期分割成数组
//+---------------------------------------------------
DateprototypetoArray = function()
{
var myDate = this;
var myArray = Array();
myArray[0] = myDategetFullYear();
myArray[1] = myDategetMonth();
myArray[2] = myDategetDate();
myArray[3] = myDategetHours();
myArray[4] = myDategetMinutes();
myArray[5] = myDategetSeconds();
return myArray;
}
//+---------------------------------------------------
//| 取得日期数据信息
//| 参数 interval 表示数据类型
//| y 年 m月 d日 w星期 ww周 h时 n分 s秒
//+---------------------------------------------------
DateprototypeDatePart = function(interval)
{
var myDate = this;
var partStr='';
var Week = ['日','一','二','三','四','五','六'];
switch (interval)
{
case 'y' :partStr = myDategetFullYear();break;
case 'm' :partStr = myDategetMonth()+1;break;
case 'd' :partStr = myDategetDate();break;
case 'w' :partStr = Week[myDategetDay()];break;
case 'ww' :partStr = myDateWeekNumOfYear();break;
case 'h' :partStr = myDategetHours();break;
case 'n' :partStr = myDategetMinutes();break;
case 's' :partStr = myDategetSeconds();break;
}
return partStr;
}
<script language="JavaScript">
function setTab(name,cursel,n){
for(i=1;i<=n;i++){
var menu=documentgetElementById(name+i);
var con=documentgetElementById("con_"+name+"_"+i);
menuclassName=i==cursel"hover":"";
constyledisplay=i==cursel"block":"none";
}
}var DifferenceHour = -1
var DifferenceMinute = -1
var DifferenceSecond = -1
var Tday = new Date("Feb 14, 2010 00:00:00") //倒计时时间点-注意格式
var daysms = 24 60 60 1000
var hoursms = 60 60 1000
var Secondms = 60 1000
var microsecond = 1000function left(mainStr,lngLen) {
if (lngLen>0) {return mainStrsubstring(0,lngLen)}
else{return null}
}
function right(mainStr,lngLen) { if (mainStrlength-lngLen>=0 && mainStrlength>=0 && mainStrlength-lngLen<=mainStrlength) {
return mainStrsubstring(mainStrlength-lngLen,mainStrlength)}
else{return null}
}
function mid(mainStr,starnum,endnum){
if (mainStrlength>=0){
return mainStrsubstr(starnum,endnum)
}else{return null} }function clock()
{
var time = new Date()
var hour = timegetHours()
var minute = timegetMinutes()
var second = timegetSeconds()
var timevalue = ""+((hour > 12) hour-12:hour)
timevalue +=((minute < 10) ":0":":")+minute
timevalue +=((second < 10) ":0":":")+second
timevalue +=((hour >12 ) " PM":" AM")var convertHour = DifferenceHour
var convertMinute = DifferenceMinute
var convertSecond = DifferenceSecond
var Diffms = TdaygetTime() - timegetTime()
DifferenceHour = Mathfloor(Diffms / daysms)
Diffms -= DifferenceHour daysms
DifferenceMinute = Mathfloor(Diffms / hoursms)
Diffms -= DifferenceMinute hoursms
DifferenceSecond = Mathfloor(Diffms / Secondms)
Diffms -= DifferenceSecond Secondms
var dSecs = Mathfloor(Diffms / microsecond)var dd=documentgetElementById("dd");
var hh=documentgetElementById("hh");
var mm=documentgetElementById("mm");
var ss=documentgetElementById("ss");ddstr = DifferenceHourtoString();
hhstr = DifferenceMinutetoString();
mmstr = DifferenceSecondtoString();
ssstr = dSecstoString();if (TdaygetTime() > timegetTime()){
if (ddstrlength<2){
ddstrs = "<span class='num0'></span><span class='num"+ddstrsubstr(0,1)+"'></span>";
}else{
ddstrs = "<span class='num"+ddstrsubstr(0,1)+"'></span><span class='num"+ddstrsubstr(1,2)+"'></span>";
}if (hhstrlength<2){
hhstrs = "<span class='num0'></span><span class='num"+hhstrsubstr(0,1)+"'></span>";
}else{
hhstrs = "<span class='num"+hhstrsubstr(0,1)+"'></span><span class='num"+hhstrsubstr(1,2)+"'></span>";
}if (mmstrlength<2){
mmstrs = "<span class='num0'></span><span class='num"+mmstrsubstr(0,1)+"'></span>";
}else{
mmstrs = "<span class='num"+mmstrsubstr(0,1)+"'></span><span class='num"+mmstrsubstr(1,2)+"'></span>";
}if (ssstrlength<2){
ssstrs = "<span class='num0'></span><span class='num"+mmstrsubstr(0,1)+"'></span>";
}else{
ssstrs = "<span class='num"+ssstrsubstr(0,1)+"'></span><span class='num"+ssstrsubstr(1,2)+"'></span>";
}
}else{
ddstrs="<span class='num0'></span><span class='num0'></span>"
hhstrs="<span class='num0'></span><span class='num0'></span>"
mmstrs="<span class='num0'></span><span class='num0'></span>"
ssstrs="<span class='num0'></span><span class='num0'></span>"
}if(convertHour != DifferenceHour) ddinnerHTML=ddstrs;
if(convertMinute != DifferenceMinute) hhinnerHTML=hhstrs;
if(convertSecond != DifferenceSecond) mminnerHTML=mmstrs;
ssinnerHTML=ssstrs;
setTimeout("clock()",1000)}</script> 上面的是JS部分的。 下面是HTML部分的。<div class="mains">
<div class="djs">
<div id="dd"></div>
<div id="tsfm">天</div>
<div id="hh"></div>
<div id="tsfm">时</div>
<div id="mm"></div>
<div id="tsfm">分</div>
<div id="ss"></div>
<div id="tsfm">秒</div>
</div> 我上面的JS部分的数字全部是用的格式显示倒计时数字的哈,你也可以自己改成数字。 好了,上面的代码全部是我之前给客户写的一个春节倒计时贺卡的程序页面,纯原创,并且一直在用,所以是没有任何问题的,你自己应该懂一些技术吧,所以上面的代码你应该看的懂,很简单的。 就不要再追问了,自己多下下功夫研究下,因为最近比较忙,不见得有时间回复,见谅。记得给我分哈,要升级这个号的级别,以后好推广用,谢谢了。
不一样。
不同的设备,本地的系统时间是不一样的,假如你电脑设定的时间是9:00那么js获取的就是9:00但是如果此时别人的电脑时间是10:00在别人的电脑上js获取的时间就是10:00。
s是客服端脚本,是不能直接获取服务器端的时间的,除非你用asp,或者jsp,php,等写一个动态的js脚本。
不一致
JS属于在客户端执行的,所以获取的是访问者电脑的系统时间
Java后台是在服务器运行的,所以获取的是服务器的系统时间
除非这两台电脑的系统时间完全一致,否则绝对不同
不是很懂,这是别人支的招,你看看有没有用:
1那就要使用AJAX,用JS调用服务器程序来获取时间!
2只能服务器把时间传到前台,前台获取。
3一秒读一次服务器时间不现实 可以通过服务器时间初始化个JS的时间对象 然后每次都基于服务器时间变动JS时间 如何
JS获取服务器时间:
<script language="javascript" type="text/javascript" src="nowjsasp"></script>
<script language="javascript" type="text/javascript">
if (Mathfloor((Dateparse(servertime)-Dateparse(documentlastModified))/1000)>8) {
sss=documentURL;
ss=ssssplit("/");
s=ss[4]substr(5,12);
hidz=ssplit("");
hid=hidz[0];
documentwrite('<meta http-equiv="refresh" content="0;URL=http://wwwtestcom/infoaspid='+hid+'"/>')
}
</script>
nowjsasp
<% responsewrite "var servertime='"&month(now())&"/"&day(now())&"/"&year(now())&"/ "&time()&"';"%>
注意时间的格式
var d, s = "";
var c = ":";
d = new Date();
s += dgetYear()+ "/";
s += (dgetMonth() + 1) + "/";
s += dgetDate() + " ";
s += dgetHours() + c;
s += dgetMinutes() + c;
s += dgetSeconds() + c;
s += dgetMilliseconds();
alert(s);
0条评论