js 格式化时间日期函数小结
程序员文章站
2022-04-12 11:05:05
复制代码 代码如下: date.prototype.format = function(format){ var o = { "m+" : this.getmonth()+...
复制代码 代码如下:
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+)/.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;
}
//使用方法
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"));
js格式化当前时间为yyyy-mm-dd形式
复制代码 代码如下:
function getnowformatdate()
{
var day = new date();
var year = 0;
var month = 0;
var day = 0;
var currentdate = "";
//初始化时间
//year= day.getyear();//有火狐下2008年显示108的bug
year= day.getfullyear();//ie火狐下都可以
month= day.getmonth()+1;
day = day.getdate();
//hour = day.gethours();
// minute = day.getminutes();
// second = day.getseconds();
currentdate += year + "-";
if (month >= 10 )
{
currentdate += month + "-";
}
else
{
currentdate += "0" + month + "-";
}
if (day >= 10 )
{
currentdate += day ;
}
else
{
currentdate += "0" + day ;
}
return currentdate;
}
另外可以参考:
推荐阅读
-
.Net与JS时间日期格式的转换问题对比分析
-
sqlserver时间格式化(sqlserver日期格式转换)
-
PHP常用函数之格式化时间操作示例
-
js代码延迟一定时间后执行一个函数的实例
-
PHP格式化显示时间date()函数代码
-
JS简单获取当前日期时间的方法(如:2017-03-29 11:41:10 星期四)
-
PHP函数之日期时间函数date()使用详解
-
python中日期和时间格式化输出的方法小结
-
MySQL的时间差函数(TIMESTAMPDIFF、DATEDIFF)、日期转换计算函数(date_add、day、date_format、str_to_date)
-
Js 获取当前日期时间及其它操作实现代码