从javascript中的时间字符串创建日期 [英] creating date from a timestring in javascript

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

问题描述

我是 javascript 新手,正在尝试比较两个日期值,我得到两个格式的时间值字符串

I am new to javascript and am trying to compare two date values ,I am getting two time value strings in the format

06:30:47 上午

06:30:47 AM

下午 1:10:47

我需要比较这些以找出第一个是否小于另一个.我不知道如何在 javascript 中执行此操作.有人可以帮忙吗?哦

I need to compare these to find out if the first one is less than the other.I couldn't figure out how to do this in javascript.Can someone help? o.h

推荐答案

我不认为标准实现可以解析这个.我会做这样的事情:

I do not think that the standard implementation can parse this. I would do something like this:

function toDate(dateString) {
    var timeComponents = dateString.replace(/\s.*$/, '').split(':');

    if (dateString.indexOf("PM") > -1) {
       timeComponents[0] += 12;
    }

    var date = new Date();
    date.setHours(timeComponents[0]);
    date.setMinutes(timeComponents[1]);
    date.setSeconds(timeComponents[2]);

    return date;
}

if (toDate('06:30:47 AM') > toDate('01:10:47 PM')) {
    // ...
}

这篇关于从javascript中的时间字符串创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆