申请博客好几个月了,一直想写一些东西但是总觉着自己写不好,就一直搁浅了。废话不多说,进入正题!
ios中和Safari浏览器中对js Date对象的支持
1.问题情形是这样的,需要在特定的时间显示不同的内容。
解决办法有两种:
1.通过后台获取服务器的时间。
在返回给前端标志,前端通过模板也好,通过其他方式进行判断也好,显示不同的内容。这个我们不管...
2.通过本地获取系统时间显示不同的内容。
我采用的就是这种方式,但是这里就出现了问题。附上代码
function setContentWithTime(time){ var current = new Date(), setDate = new Date(time).getTime(); var curTime = new Date(current.getFullYear()+'-'+(current.getMonth()+1)+'-'+current.getDate()).getTime(); if(curTime == setDate){ ... }else{ ... } }
setContentWithTime("2015-8-13");
上面这段代码在chrome和ff里面跑都没有问题。但是在safari里面跑就出现了问题。
问题如下:
ok,问题就出现了,在safari里面是无法对形如"xxxx-xx-xx"这种形式的日期进行变换,在safari里面
4.在那些ios系统中是不支持js 的Date()这种转换的。请大家测试一下,本人知道的是在ios8.4里面仍然存在这个问题。
5.提到这里我们也可以再次查看js Date对象里面的构造方法会发现。其实我们可以采取另外的方法进行new Date;
查资料发现Date的构造函数:new
Date
(
)
new
Date
(
milliseconds
)
new
Date
(
datestring
)
new
Date
(
year
,
month
)
new
Date
(
year
,
month
,
day
)
new
Date
(
year
,
month
,
day
,
hours
)
new
Date
(
year
,
month
,
day
,
hours
,
minutes
)
new
Date
(
year
,
month
,
day
,
hours
,
minutes
,
seconds
)
new
Date
(
year
,
month
,
day
,
hours
,
minutes
,
seconds
,
microseconds
)
这里需要注意的是:
- 如果没有任何参数,将返回当前日期
- 如果参数为一个数字,将数字视为毫秒值,转换为日期
- 如果参数为一个字符串,将字符串视为日期的字符串表示,转换为日期
- 还可以使用六个构造函数精确定义,并返回时间
结语:其实是个小问题,但是在做项目的过程中,总是让人头疼,请大家勿喷!!!谢谢!
参考文献:
http://www.w3school.com.cn/jsref/jsref_obj_date.asp
http://blog.sina.com.cn/s/blog_6a0cd5e501011so7.html
http://www.cnblogs.com/keke/p/3372558.html
http://blog.csdn.net/liu510817387/article/details/8688216