在js中将时间戳按照指定格式在jsp页面输出

本文介绍了一种在JavaScript中自定义日期格式的方法。通过扩展Date.prototype,实现了一个toLocaleString方法,可以将日期转换为指定格式的字符串,包括年、月、日、时、分、秒,并确保个位数前面补零。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js中:

Date.prototype.toLocaleString = function() {
		//月
		var moth=(this.getMonth() + 1);
		//日
		var date=this.getDate();
		//小时
		var hours=this.getHours();
		//分
		var minutes=this.getMinutes();
		//秒
		var sconds=this.getSeconds()
		if(moth>=1&&moth<=9){
		 moth="0"+moth;
		}
		if(date>=0&&date<=9){
			date="0"+date;
		}
		if(hours>=0&&hours<=9){
			hours="0"+hours;
		}
		if(minutes>=0&&minutes<=9){
			minutes="0"+minutes;
		}
		if(sconds>=0&&sconds<=9){
			sconds="0"+sconds;
		}
        return this.getFullYear() + "-" +moth+ "-" +date+ " " +hours+ ":" +minutes+ ":" +sconds;
  	};
var unixTimestamp = new Date(checkOutMessage.checkintime) ;
commonTime = unixTimestamp.toLocaleString();
 alert(commonTime);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值