Home > Web Front-end > JS Tutorial > body text

js用Date对象处理时间实现思路及代码_基础知识

WBOY
Release: 2016-05-16 17:42:27
Original
973 people have browsed it
先说说这次的应用场景
从XML文件中读取数据,然后将里面的数据返回到页面中用一个第三方插件进行绘图。

在XML里读取出来的数据当然是字符串类型,而如果希望使用绘图插件X轴的time mode(这样才能更合理的定制如x轴范围、x轴数据format等)。则必须将数据转化为标准的时间对象。这个时候js里内置的Date类型就派上了用场:
复制代码 代码如下:

var date = new Date();
//id为time的input域包含了xml里提取的时间信息
date.setHours($("#time" ).val().substring(0, 2)+8);
date.setMinutes($("#time" ).val().substring(2, 4));
date.setSeconds($("#time" ).val().substring(4, 6));

XML里的时间类型是小时分钟秒的格式(如083100),将里面的数据取出来之后就可以用诸如setHours、setMinutes、setSeconds等设置时间的函数来实例化Date对象。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!