您当前的位置:首页 > 互联网教程

在引用js文件timestamp作用是什么

发布时间:2025-05-19 16:08:14    发布人:远客网络

在引用js文件timestamp作用是什么

一、在引用js文件timestamp作用是什么

为了避免缓存引起的最新js无法加载,所以需要用timestamp来表示。

1、在<script type="text/javascript" src="/js/common.js"></script>在后面加一个时间戳来解决。这样url地址每次变化,浏览器就会请求服务端的js,而不会使用缓存。保证页面每次加载到的都是最新的js文件。

加版本号,js有个版本。如果每次发布新的js代码。后面就会附加新的版本号。然后用户加载html页面的时候。版本号附加在在

<script type="text/javascript" src="/js/common.js?v=99"></script>

这样也可以保证js同步加载到最新的版本。

二、js怎么把toLocaleString转成时间戳

1、不是很确定的你的目的,但是以我的经验这样做法可能不够正确,越是跨地区的越是需要时间戳。我只能猜测到你可能希望在美国和德国等等地区的用户只显示中国当地的时间。但是你这种的做法也可能是错误。应该是var nowtime=(new Date().toLocaleString('zh',{ timeZone:'asia/shanghai', hours12: false})),加上timeZone才能让海外的用户得到准确的中国时间;而且你可以先获取时间戳再做本地时间转换。

2、var timestamp= date.getTime();

3、var nowTime= date.toLocaleString('zh',{ timeZone:'asia/shanghai', hours12: false})

4、如果是后端传给你就是字符串的日期,你要他们尽量给你时间戳。如果是自己拿个时间戳应该不是什么问题。

5、如果你非得这样做必须替换掉PM或者下午这些词。我做的很粗暴,随便看看就好。

6、时间比较复杂的话建议用moment.js和day.js

7、var nowtime=(new Date()).toLocaleString(('zh',{ hour12: false}))

8、function localeDateStringtoTimestamp(dateString){

9、 if(dateString.split('上午').length== 2){

10、 var _date= dateString.split('上午').join("");

11、 return new Date(_date).getTime()

12、 if(dateString.split('下午').length== 2){

13、 var _dateArray= dateString.split('下午');

14、 var hms= _dateArray[1].split(":");

15、 hms[0]= parseInt(hms[0])< 10?(parseInt(hms[0])+ 12)+'': hms[0];

16、 return new Date(ymd+ hms.join(':')).getTime();

17、console.log(localeDateStringtoTimestamp(nowtime))

三、js new Date() 格式

对 new Date()得到日期的进行格式显示扩展,扩展方法如下:

Date.prototype.Format= function(fmt){//author: meizz

"M+": this.getMonth()+ 1,//月份

"q+": Math.floor((this.getMonth()+ 3)/ 3),//季度

"S": this.getMilliseconds()//毫秒

if(/(y+)/.test(fmt)) fmt= fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-

if(new RegExp("("+ k+")").test(fmt)) fmt= fmt.replace(RegExp.$1,(RegExp.$1.length== 1)?

(o[k]):(("00"+ o[k]).substr((""+ o[k]).length)));

(new Date()).Format("yyyy-MM-dd hh:mm:ss.S")//输出结果: 2017-01-23 09:36:10.400

(new Date()).Format("yyyy-M-d h:m:s.S")//输出结果: 2017-1-23 9:36:35.572

JS Date对象常用的带参数初始化方式:

var date1= new Date(2017,06,06); console.log(date1);// Thu Jul 06 2017 00:00:00 GMT+0800(中国标准时间)

var date1= new Date(2017,1,1); console.log(date1);// Wed Feb 01 2017 00:00:00 GMT+0800(中国标准时间)

var date1= new Date(2017,01-2,01); console.log(date1);// Thu Dec 01 2016 00:00:00 GMT+0800(中国标准时间)

var date1=new Date(2017,06,06,06,06,06); console.log(date1);// Thu Jul 06 2017 06:06:06 GMT+0800(中国标准时间)

说明: new Date( year, month, date, hrs, min, sec)按给定的参数创建一日期对象

var date2= new Date(“2017/06/06”); console.log(date2);// Tue Jun 06 2017 00:00:00 GMT+0800(中国标准时间)

var date2= new Date(“2017-08-08”); console.log(date2);// Tue Aug 08 2017 08:00:00 GMT+0800(中国标准时间)

var date2= new Date(“2017-9-9”); console.log(date2);// Sat Sep 09 2017 00:00:00 GMT+0800(中国标准时间)

说明:如果字符串模式不支持短横杠模式,则进行字符串替换:

var date2= new Date(Date.parse(strTime.replace(/-/g,“/”)));///-/g为正则表达式(RegExp)对象,表示全局替换-为/。

参考资料来源:百度百科- Date()

参考资料来源:百度百科- javascript