js 获取服务器时间和new Date()用法

来源:互联网 发布:淘宝信用卡办理条件 编辑:程序博客网 时间:2024/06/05 04:13

我们经理给提了一个需求就是到某一个固定的时间就需要一个按钮不显示了,如果试用new Date() 这个获取的是客户端的时间不是服务器时间,客户如果改了本机的时间那这个按钮就没有控制住。找了很多前辈的代码我自己也总结一下,下次使用的时候也方便我查找,也给不知道的小伙伴分享一下。

1.获取服务器时间:
var now = new Date($.ajax({async: false}).getResponseHeader("Date"));
2.new Date()用法:
获取年:var currentYear = now.getFullYear();获取月:var currentMonth = now.getMonth();获取日:var currentDay = now.getDate();获取小时:var currentHours = now.getHours();获取分钟:var currentMinutes = now.getMinutes();获取秒:var currentSeconds = now.getSeconds();获取毫秒:var currentMilliseconds = now.getMilliseconds();获取当前日期:var currentDate = now.toLocaleDateString();获取当前时间:var currentTime = now.toLocaleTimeString();获取日期和时间:var dateTime = now.toLocaleString();获取当前星期几:var currentWeek = now.getDay();获取完成年份:var currentFullYear = now.getFullYear();
原创粉丝点击