.Net(C#)在JS中获取当前服务器时间
草,这么简单!
我有,给你!不过要等我老板走了。。。
老板走了给你贴
function TimeNow() {
var Time = new Date();
var year = TimegetYear();
var month = TimegetMonth() + 1;
var day = TimegetDate();
var hour = TimegetHours();
var minute = TimegetMinutes();
var second = TimegetSeconds();
return shijian = year + "\u5e74" + month + "\u6708" + day + "\u65e5" + " " + hour + ":" + minute + ":" + second;
}
以上存为 DATEjs
<html>
<script type="text/javascript" src="DATEjs"></script>
<script type="text/javascript">
function ShowTime()
{
var time = TimeNow();
var divid = documentgetElementById("time");
dividinnerHTML = "<font color='green'>"+time+"</font>";
setTimeout('ShowTime();',1000);
}
</script>
</style>
<body onload="ShowTime();">
<div id="time">
</div>
</body>
</html>
以上存为:任意名html
调用JS的过程就不解释了,
<body></body>之间是显示与调用
希望对你有用!
这个
后台
代码
应该是ASPNET开发的
页面
的C#代码,
可以直接放在aspx页面的<script
runat="server"></script>
区块
内;
在cs代码与aspx代码分离的情况下,
这个后台代码应该放在c#代码页面内
在页面上加代下面代码就能用了然后你根据实际位置改一下
<script type="text/javascript">
function GetCurDate()
{
var strClock = "";
var isnMonth = new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
var isnDay = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六","星期日");
var today = new Date();
strYear=todaygetYear();
strDate=todaygetDate();
strClock = strYear+"年"+isnMonth[todaygetMonth()]+strDate+"日 ";
sec = todaygetSeconds();
hr = todaygetHours();
min = todaygetMinutes();
if (hr <= 9) hr = "0" + hr;
if (min <= 9) min = "0" + min;
if (sec <= 9) sec = "0" + sec;
strClock += " " + hr + ":" + min + ":" + sec;
ClockTextinnerText = strClock;
}
function GoForIt()
{
if (documentall) setInterval("GetCurDate()",1000)
}
</script>
[<span id="ClockText"></span><script type="text/javascript" language="JavaScript">GoForIt();</script>]
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;
}
0条评论