简体   繁体   English

javascript日期和时间加入

[英]javascript date & time join

I have two date variable separately like following 我分别有两个日期变量,如下所示

startDate is a Date instance with the value Tue Jul 17 2012 00:00:00 GMT+0530 (IST) startDate是一个Date实例,其值为Tue Jul 17 2012 00:00:00 GMT+0530 (IST)

startTime is a String with the value "11:30 AM" startTime是一个String ,其值为"11:30 AM"

Now what I need is join of both above date & time, as a Date . 现在,我需要将上述日期和时间都作为Date

startDateTime = Tue Jul 17 2012 11:30:00 GMT+0530 (IST) startDateTime = Tue Jul 17 2012 11:30:00 GMT+0530 (IST)

I tried 我试过了

new Date(startDate + " " + startDate) but outputting invalid date . new Date(startDate + " " + startDate)但输出invalid date

Also tried the way shown on this post . 还尝试了此帖子中显示的方式。 But still not working. 但仍然无法正常工作。

You can readily parse startTime if it's in a clearly-defined format, then use setHours and setMinutes : Live example | 如果startTime采用明确定义的格式,则可以轻松地对其进行解析,然后使用setHourssetMinutes实时示例 | source 资源

var startDateTime;
var parts = /^(\d+):(\d+) (AM|PM)$/.exec(startTime);
if (parts) {
    hours = parseInt(parts[1], 10);
    minutes = parseInt(parts[2], 10);
    if (parts[3] === "PM" && hours !== 12) {
        hours += 12;
    }
    else if (parts[3] === "AM" && hours === 12) {
        hours = 0;
    }
    if (!isNaN(hours) && !isNaN(minutes)) {
        startDateTime = new Date(startDate.getTime());
        startDateTime.setHours(hours);
        startDateTime.setMinutes(minutes);
    }
}

...or something along those lines. ...或类似的规定。

Note that key to this is the fact you've said startDate is a Date instance. 请注意,关键在于您已经说过startDateDate实例。 The above assumes we're working within the timezone of the JavaScript environment, not across zones. 以上假设我们正在JavaScript环境的时区内工作,而不是跨区域。 If you were starting with a date string instead, and that string specified a timezone other than the JavaScript environment's timezone, which you were then converting into a Date via new Date("Tues Jul....") , then you'd have to be sure to adjust the resulting Date to use either the local time of the environment, or UTC; 如果您改用日期字符串开头,并且该字符串指定了JavaScript环境时区以外的时区,然后通过new Date("Tues Jul....")将其转换为Date ,则您需要确保将结果Date调整为使用环境的本地时间或UTC; if you adjusted it to be UTC, you'd use setUTCHours and setUTCSeconds above instead of setHours and setSeconds . 如果你调整它是UTC,你会使用setUTCHourssetUTCSeconds上面,而不是setHourssetSeconds Again, this is only an issue if your starting point is a date string , and that string specifies a timezone different from the timezone in which the code above is running. 同样,这仅是一个问题,如果您的起点是日期字符串 ,并且该字符串指定的时区上面的代码在其中运行的时区不同

You can do This: 你可以这样做:

var theDate = new Date("Tue Jul 17 2012 00:00:00 GMT+0530 (IST)");
var theTime = "11:30 AM";
var hours = theTime .substr(0,2);
var minutes = theTime .substr(3,2);
var amOrPm = theTime .substr(6,2);
if (hours < 12 && "PM" == amOrPm) {
    hours = +hours + 12;
}
theDate.setHours(hours);
theDate.setMinutes(minutes);

Try 尝试

new Date(startDate.toDateString() + " " + startTime)

This combines the date string from your Date object with the time string, and should give you a valid date. 这会将Date对象中的日期字符串与时间字符串结合在一起,并应给您一个有效的日期。 Note that this ignores the timezone you initially worked with, you might need to add " GMT+0530" again. 请注意,这会忽略您最初使用的时区 ,您可能需要再次添加" GMT+0530"

However, because your date string is already timezone-biased (Jul 16 2012, 20:30:00 UTC) it might be better to add them together, ie like new Date(+startDate + milliseconds) : 但是,由于您的日期字符串已经是时区偏移的(Jul 16 2012,20:30:00 UTC),所以最好将它们添加在一起,例如,像new Date(+startDate + milliseconds)

var startDate = new Date("Tue Jul 17 2012 00:00:00 GMT+0530");
var startTime = "11:30 AM";
return new Date(+startDate + +new Date("1 1 1970 "+startTime))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM