=================yyyy/mm/dd 下午hh:mm:ss
$(function(){
var mydate = new Date();
var t=mydate.toLocaleString();
$("#time").text(t);
});
<P id="time">afwef</P>
======================yyyy年mm月dd日
function show(){
var mydate = new Date();
var str = "" + mydate.getFullYear() + "年";
str += (mydate.getMonth()+1) + "月";
str += mydate.getDate() + "日";
return str;
}
alert ( show() );
=====================具体获取函数
获取JavaScript 的时间使用内置的Date函数完成
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( ); //获取日期与时间
日期时间脚本库方法列表
Date.prototype.isLeapYear 判断闰年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期计算
Date.prototype.DateDiff 比较日期差
Date.prototype.toString 日期转字符串
Date.prototype.toArray 日期分割为数组
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天数
Date.prototype.WeekNumOfYear 判断日期所在年的第几周
StringToDate 字符串转日期型
IsValidDate 验证日期有效性
CheckDateTime 完整日期时间检查
daysBetween 日期天数差
================重构
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
(((=== 你现在应该懂了,但是我为了后面的人还总结得列一下:
for (var k in o)这个K不知道代表什么 //K是O里面的各个属性,即m+ d+ h+.....可以理解为索引
RegExp("(" + k + ")")这个K代表着字母 //这是字符串拼接 将(K)作为参数生成新的正则,为了实现RegExp.$1等正则对象,故加()。
o[k]我觉得这个K代表数字 //这个代表o里面K这个属性,选取到这个属性。
这三个K的意思是一样的吗?//都代表o里面的属性对象
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
//这行代码的意思是:如果 new RegExp("(" + k + ")") 这个用o里面属性新创建的正则对象在 fmt这个从外面传进来的参数里 存在(一般fmt是日期格式如 yyyy-MM-dd HH:mm:SS,则将fmt替换为传进来对应的日期格式。
=====================多有不妥之处,还望指出。---小李飞刀燕子抄水 )))
//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().format("yyyy年MM月dd日"));
alert(new Date().format("MM/dd/yyyy"));
alert(new Date().format("yyyyMMdd"));
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
===========================
格式一:(yyyy-MM-dd HH:mm:SS)
[javascript] view plain copy
- function getFormatDate(){
- var nowDate = new Date();
- var year = nowDate.getFullYear();
- var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
- var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
- var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
- var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
- var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
- return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
- }
- var str = getFormatDate();
- console.log(str);
格式二:(yyyy-MM-dd)
[javascript] view plain copy
- function getFormatDate(){
- var nowDate = new Date();
- var year = nowDate.getFullYear();
- var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
- var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
- // var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
- // var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
- // var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
- return year + "-" + month + "-" + date;
- }
- var str = getFormatDate();
- console.log(str);
-
来源:http://blog.csdn.net/Gemini_llw/article/details/76412053?locationNum=8&fps=1
http://www.cnblogs.com/LiuJL/p/5417685.html