Javascript获取日期的函数(很老很土,省的每次查了)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyHtml.html</title>

<script type="text/javascript">
<!--

/**
* 获取年、月、日、时、分、秒、毫秒
*/
var nowDate = new Date();
var nowYear = nowDate.getFullYear(); // 从 Date 对象以四位数字返回年份。请注意“getYear() + 1900”那种方式已经不推荐使用
var nowMonth = nowDate.getMonth() + 1; // 从 Date 对象返回月份 (0 ~ 11)。所以加1
var nowDay = nowDate.getDate(); // 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
var nowWeek = nowDate.getDay(); // 从 Date 对象返回一周中的某一天 (0 ~ 6)。
var nowHour = nowDate.getHours(); // 返回 Date 对象的小时 (0 ~ 23)。
var nowMinute = nowDate.getMinutes(); // 返回 Date 对象的分钟 (0 ~ 59)。
var nowSec = nowDate.getSeconds(); // 返回 Date 对象的秒数 (0 ~ 59)。
var nowMilliSec = nowDate.getMilliseconds(); // 返回 Date 对象的毫秒(0 ~ 999)。

document.write(nowYear + "年");
document.write(nowMonth + "月");
document.write(nowDay + "日");
document.write(nowHour + "时");
document.write(nowMinute + "分");
document.write(nowSec < 10 ? ("0" + nowSec) : nowSec + "秒");
document.write(nowMilliSec + "毫秒");

/**
* 其他常用的
*/
document.write('<br />' + nowDate.toLocaleString()); // 根据本地时间格式,把 Date 对象转换为字符串。
document.write('<br />' + nowDate.toLocaleDateString()); // 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
document.write('<br />' + nowDate.toLocaleTimeString()); // 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
document.write('<br />' + nowDate.getTime()); // 返回 1970 年 1 月 1 日至今的毫秒数。

//-->
</script>
</head>

<body>

</body>
</html>




输出为:

2010年4月16日9时56分45秒190毫秒
Fri Apr 16 2010 09:56:45 GMT+0800 (China Standard Time)
Friday, April 16, 2010
09:56:45
1271383005190



更多的自己看api
http://www.w3school.com.cn/js/jsref_obj_date.asp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值