本文介绍了javascript:如何解析日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

格式为:
MMDDHHMM

The format is:MMDDHHMM

我想单独考虑月,日,小时,分钟,怎么做?

I want to take month, day, hour, minute individually, how to do that?

推荐答案

var dateString = '13011948';

文本的长度是固定的,并且始终位于相同的位置。然后你可以使用 substr 将它们分成几部分并使用 parseInt 将它们转换为数字。

The length of the text is fixed and always at the same position. Then you can just use substr to cut them into parts and use parseInt to convert them to number.

var month = parseInt(dateString.substr(0, 2), 10),
      day = parseInt(dateString.substr(2, 2), 10),
     hour = parseInt(dateString.substr(4, 2), 10),
   minute = parseInt(dateString.substr(6, 2), 10);

或者将它放在一个日期对象中。

Or instead, put it in a single date object.

var date = new Date();
date.setMonth   (parseInt(dateString.substr(0, 2), 10) - 1);
date.setDate    (parseInt(dateString.substr(2, 2), 10));
date.setHours   (parseInt(dateString.substr(4, 2), 10));
date.setMinutes (parseInt(dateString.substr(6, 2), 10));

这篇关于javascript:如何解析日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:56