JS获取当前日期时间

本文介绍了使用JavaScript获取系统日期的函数实现,包括年、月、日、小时、分钟、秒等信息。

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

		//取得系统日期
		function nowDatetime() {
		
			var date = new Date();
			var month = (date.getMonth()+1) > 9 ? (date.getMonth()+1) : "0" + (date.getMonth()+1);
			var day = (date.getDate()) > 9 ? (date.getDate()) : "0" + (date.getDate());
			var hours = (date.getHours()) > 9 ? (date.getHours()) : "0" + (date.getHours());
			var minutes = (date.getMinutes()) > 9 ? (date.getMinutes()) : "0" + (date.getMinutes());
			var seconds = (date.getSeconds()) > 9 ? (date.getSeconds()) : "0" + (date.getSeconds());
			
			var dateString = 
				date.getFullYear() + "/" + 
			    month + "/" + 
			    day + " " + 
			    hours + ":" + 
			    minutes + ":" + 
			    seconds;
			    
			return dateString;  
		}
var date = new Date();  
date.getYear();        //获取当前年份(2位)  
date.getFullYear();    //获取完整的年份(4位,2014)  
date.getMonth();       //获取当前月份(0-11,0代表1月)  
date.getDate();        //获取当前日(1-31)  
date.getDay();         //获取当前星期X(0-6,0代表星期天)  
date.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)  
date.getHours();       //获取当前小时数(0-23)  
date.getMinutes();     //获取当前分钟数(0-59)  
date.getSeconds();     //获取当前秒数(0-59)  
date.getMilliseconds();    //获取当前毫秒数(0-999)  
date.toLocaleDateString();     //获取当前日期   如 2014年6月25日   
date.toLocaleTimeString();     //获取当前时间   如 下午4:45:06  
date.toLocaleString();         //获取日期与时间 如 2014年6月25日 下午4:45:06  

 

转载于:https://www.cnblogs.com/puxian/p/5123674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值