js 格式化字符串为时间yyyy-MM-dd
document.write(myDate.split(" ",1));myDate.getDate(); //获取当前日(1-31)
js获取当前日期并格式化_js获取当前时间年月日并格式化
HTML如何显示当前动态时间?
myDate.getFullYear(); //获取完整的年份(4位,1970-????)function 时间程序() {
function IsValidDate(DateStr)var date = new Date();
function CheckDateTime(str)var year = date.getFullYear();
var month = 时间格式化(date.getMonth() + 1,2);
var day = 时间格式化(date.getDate(),2);
var week = "星期" + "日一二三四五六".charAt(date.getDay());
var hour = 时间格式化(date.getHours(),2);
var minute = 时间格式化(date.getMinutes(),2);
var second = 时间格式化(date.getSeconds(),2);
var Milliseconds = 时间格式化(date.getMilliseconds(),3);
var currentTime = year + "-" + month + "-" + day + " " +
hour + ":" + minute + ":" + second + "." + Milliseconds + " " +
week ;
setInterval("时间程序()", 100);
return currentTime;
}function 时间格式化(参数, 位数) {
return (Array(位数).join(0) + 参数).sl(-位数)
}
js 字符串转日期时间格式
}var isnMonths=new initArray("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");你可以把当前日期转换成规定的字符串格式
//格式化日期,2010-1-10 12:30:20d="0"+this.getDate();
function formatDate() {
var d=new Date(); // 获取当前日期
var str=d.getYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+var today;d.getSeconds();
return str;
}
JS里面怎么获取当天日期
var day = date.getDate();}timeString = years+months+days+hours+minutes+seconds;
var myDate = new Date();
myDate.toLocaleTimeString(); 可以获取当前时间
扩展:
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTimyDate.getSeconds(); //获取当前秒数(0-59)me(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleString( ); //获取日期与时间
js怎么把时间转换成年月日时分秒的形式
在页面上加代下面代码就能用了.然后你根据实际位置改一下function dateFtt(fmt,date)
{ //author: meizz
var o = {
"M+" : date.getMonth()+1, //月份
"d+" : date.getDate(), //日
"m+" : date.getMinutes(), //分
"s+" : date.getSeconds(), //秒
"q+" : Math.floor((date.getMonth()+3)/3), //季度
"S" : date.getMilliseconds() //毫秒 };
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (date.gemonths = intMonths +"-";tFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).len{var str = formatStr;gth)));
return fmt;
var crtTime = new Date(value); return top.dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);//直接调用公共JS里面的时间类处理的办法 }
如果你红框内返回的是毫秒数,你就要先转换为date类型 比如红框内是X ;
var now = new Date(X);
var year = now.getFullYear(); //获取年份
var month = now.getMonth(); //获取月份 月份要+1
var hour = now.getHours(); //获取时
var minu = now.getMinutes(); //获取分钟
var sec = now.getSeconds(); //获取秒钟
这些都是原生的js方法
var time1 = new Date().format("yyyy-MM-dd hh:mm:ss");这种就行,你试试
一个简单到问题,nodejs到时间怎么格式化
+ seperator2 + date.getSeconds();项目中需要使用js格式化输出时间,发现js中并没有现成的类似PHP中date()的函数。于是用js模拟一个方便以后使用,代码如下:
格式化时间
参数: formatStr 格式化串 y年,m月,d日,h小时,i分钟,s秒钟 缺省值 "y-m-d h:i:s"
fdate 要格式化的时间(时间戳)UTC秒数 缺省值 当前时间
实例: formatDate() 当前时间默认格式 如 2011-4-12 12:51:12
formatDate('y/m/d', 2132132131) 某时间格式为 年月日 如 2010/12/5
function formatDate(formatStr, fdate)
{var fTime, fStr = 'ymdhis';
if (!formatStr)
formatStr= "y-m-d h:i:s";
if (fdate)
fTime = new Date(fdate);
fTime = new Date();
var fcase 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())12) - (dtStart.getMonth()+1);ormatArr = [
fTime.getFullYear().toString(),
(fTime.getMonth()+1).toString(),
fTime.getDate().toString(),
fTime.getHours().toString(),
fTime.getMinutes().toString(),
fTime.getSeconds().toString()
]for (var i=0; i {formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]); }return formatStr; }if(month.toString().length == 1){ 1、使用new Date()获取当前日期,new Date().getTime()获取当前毫秒数 2、计算公式,等于获取的当前日期减去或者加上一天的毫秒数。一天的毫秒数的计算公式:24小时60分钟60秒1000毫秒,也是86400000毫秒。 举例: Date curDate = new Date(); var preDate = new Date(curDate.getTime() - 2460601000); //前一天 var nextDate = new Date(curDate.getTime() + 2460601000); //后一天 以下使用后台输出表示。 扩展资料 var myDate = new Date(); myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours(); //获取当前小时数(0-23) myDate.getMinutes(); //获取当前分钟数(0-59) myDate.getMilliseconds(); //获取当前毫秒数(0-999) myDate.toLocaleDateString(); //获取当前日期 var mytime=myDate.toLocaleTimeString(); //获取当前时间 Date.protovar partStr='';type.isLeapYear 判断闰年 Date.prototype.DateAdd 日期计算 Date.prototype.DateDiff 比较日期 Date.prototype.toString 日期转字符串 Date.prototype.DatePart 取日期的部分信息 Date.prototype.MaxDayOfDate 取日期所在月的天数 Date.prototype.WeekNumOfYear 判断日期所在年的第几周 IsValidDate 验证日期有效性 CheckDateTime 完整日期时间检查 daysBetween 日期天数 JS获得当前时间的方法是利用js的date对象并格式化输出。 具体举例如下: 1、定义一个date对象today var today = new Date(); 2、分别从today中获取日,月,年,时,分,秒: var dd = today.getDate(); var mm = today.getMonth()+1; //一月是0,一定要注意 var yyyy = today.getFullYear(); var hour = today.getHours(); var minutes = today.getMinutes() ; var seconds = today.getSeconds(); 3、特殊处理,如果是日或者月小于10,自动补0 if(dd<10) { dd='0'+dd }if(mm<10) { mm='0'+mm }4、重新组装带/格式的日期和时间 today = mm+'/'+dd+'/'+yyyy+“ ”hour +“:”+minutes +":"+seconds 5、输出展示 document.write(today); Js获取当前日期时间及其它作 var myDate = new Date(); myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours(); //获取当前小时数(0-23) myDate.getMinutes(); //获取当前分钟数(0-59) myDate.getMilliseconds(); //获取当前毫秒数(0-999) myDate.toLocaleDateString(); //获取当前日期 var mytime=myDate.toLocaleTimeString(); //获取当前时间 日期时间脚本库方法列表 Date.prototype.isLeapYear 判断闰年 Date.prototype.DateAdd 日期计算 Date.prototype.DateDiff 比较日期 Date.prototype.toString 日期转字符串 Date.prototype.DatePart 取日期的部分信息 Date.prototype.MaxDayOfDate 取日期所在月的天数 Date.prototype.WeekNumOfYear 判断日期所在年的第几周 IsValidDate 验证日期有效性 CheckDateTime 完整日期时间检查 daysBetween 日期天数 js代码: //--------------------------------------------------- // 判断闰年 //--------------------------------------------------- Date.prototype.isLeapYear = function() {return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))); }//--------------------------------------------------- // 日期格式化 // 格式 YYYY/yyyy/YY/yy 表示年份 // MM/M 月份 // W/w 星期 // dd/DD/d/D 日期 // hh/HH/h/H 时间 // mm/m 分钟 // ss/SS/s/S 秒 //--------------------------------------------------- Date.prototype.Format = function(formatStr) var Week = ['日','一','二','三','四','五','六']; str=str.replace(/yyyy|YYYY/,this.getFullYear()); str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth()); str=str.replace(/M/g,this.getMonth()); str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate()); str=str.replace(/d|D/g,this.getDate()); str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours()); str=str.replace(/h|H/g,this.getHours()); str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes()); str=str.replace(/m/g,this.getMinutes()); str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds()); str=str.replace(/s|S/g,this.getSeconds()); return str; //| 求两个时间的天数 日期格式为 YYYY-MM-dd function daysBetween(DateOne,DateTwo) {var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000); return Math.abs(cha); //| 日期计算 var dtTmp = this; switch (strInterval) { case 's' :return new Date(Date.parse(dtTmp) + (1000 Number)); case 'n' :return new Date(Date.parse(dtTmp) + (60000 Number)); case 'h' :return new Date(Date.parse(dtTmp) + (3600000 Number)); case 'd' :return new Date(Date.parse(dtTmp) + (86400000 Number)); case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 7) Number)); case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), case 'y' :return new Date((dtTmp.getFullYear() + Number), }} //| 比较日期 dtEnd 格式为日期型或者有效日期格式字符串 Date.prototype.DateDiff = 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 'y' :return dtEnd.getFullYear() - dtStart.getFullYear(); }} //| 日期输出字符串,重载了系统的toString方法 Date.prototype.toString = function(showWeek) {var myDate= this; var str = myDate.toLocaleDateString(); if (showWeek) {var Week = ['日','一','二','三','四','五','六']; str += ' 星期' + Week[myDate.getDay()]; }return str; //| 日期合法性验证 //| 格式为:YYYY-MM-DD或YYYY/MM/DD {var sDate=DateStr.replace(/(^\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 = sDate.replace(/[\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(sDate.replace(/\-/g,'/')); var ar = sDate.split(/[-/:]/); if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate()) {//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); return false; }} {//alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); return false; }return true; //| 日期时间检查 //| 格式为:YYYY-MM-DD HH:MM:SS var r = str.match(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(d.getFullYear()!=r[1])return false; if(d.getMonth()!=r[2])return false; if(d.getDate()!=r[3])return false; if(d.getHours()!=r[4])return false; if(d.getMinutes()!=r[5])return false; if(d.getSeconds()!=r[6])return false; return true; //| 把日期分割成数组 Date.prototype.toArray = function() {var myDate = this; var myArray = Array(); myArray[0] = myDate.getFullYear(); myArray[2] = myDate.getDate(); myArray[3] = myDate.getHours(); myArray[5] = myDate.getSeconds(); return myArray; //| 取得日期数据信息 //| 参数 interval 表示数据类型 //| y 年 m月 d日 w星期 ww周 h时 n分 s秒 Date.prototype.Datvar nowDate = now.getDate();ePart = function(interval) {var myDate = this; var Week = ['日','一','二','三','四','五','六']; switch (interval) {case 'y' :partStr = myDate.getFullYear();break; case 'd' :partStr = myDate.getDate();break; case 'w' :partStr = Week[myDate.getDay()];break; case 'ww' :partStr = myDate.WeekNumOfYear();break; case 'h' :partStr = myDate.getHours();break; case 'n' :partStr = myDate.getMinutes();break; case 's' :partStr = myDate.getSeconds();break; }return partStr; } var myDate = new Date(); myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours(); //获取当前小时数(0-23) myDate.getMinutes(); //获取当前分钟数(0-59) myDate.getMilliseconds(); //获取当前毫秒数(0-999) myDate.toLocaleDateString(); //获取当前日期 var mytime=myDate.toLocaleTimeString(); //获取当前时间 var oDate = 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( ); //获取日期与时间 JaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。 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=today.getYear(); strDate=today.getDate(); strClock = strYear+"年"+isnMonth[today.getMonth()]+strDate+"日 "; sec = today.getSeconds(); hr = today.getHours(); min = today.getMinutes(); if (hr <= 9) hr = "0" + hr; if (min <= 9) min = "0" + min; if (sec <= 9) sec = "0" + sec; strClock += " " + hr + ":" + min + ":" + sec; ClockText.innerText = strClock; }function GoForIt() {if (document.all) setInterval("GetCurDate()",1000) } [] 次查询出的时间,然后用jS的定时器来实现秒减,就不用多次查询服务了。只是次查询而以。 不理解可再详问。 我说的方法是调用的时间啊。老兄。这个你也看不懂啊? 再看小学第二册 function disptime() {var now= new Date(); var year=now.getYear(); var month=now.getMonth()+1; var day=now.getDate(); var hour=now.getHours(); var minute=now.getMinutes(); var second=now.gemyDate.getYear(); //获取当前年份(2位)tSeconds(); document.getElementById("myTime").value=year+"-"+month+"-"+day+" "+hour+":"+":"+minute+":"+second; setTimeout("disptime()",1000);//回调函数 1000等于1秒 按毫秒计算 }再在body里面调用方法就可以了!js如何去当前时间前一天的时间
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));js 中怎么获取当前系统时间 ?
myArray[4] = myDate.getMinutes();.Net(C#)用JS怎么获取当前时间
str=str.replace(/w|W/g,Week[this.getDay()]);
我家里电脑是有这个的源码 你要是不着急的话HI我一下我晚上给你
像这样的话。你觉得你还不如用AJAX无刷新技术。。。
html页面怎么获取当前时间
s += (d.getMonth() + 1) + "/"; // 获取月份。使用js的Date对象,具体使用方法可以直接搜索 js Date。这个时间是当前访问者使用的设备的当前时间,而不是端的时间,所以这个时间通常用于UI的优化辅助,如果你需要知道准确的时间应该从端获取。
}-------------------看效果这样------dtTmp.getSeconds());--------版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 12345678@qq.com 举报,一经查实,本站将立刻删除。