javascript - Jquery How to check today time greater then use given time? - Stack Overflow

How to check time conditionin Jqueryvar startTime="20:02:55";or 12:34var endTime ="2

How to check time conditionin Jquery

 var startTime="20:02:55"; // or 12:34
 var endTime ="21:02:55"  // or 1:34 
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();

if(time >startTime || time < endTiime){
 $("#a").html("show Box");
}else{
 $("#a").html("Expire BOx");
}

How to check 12 hour and 24 hour condition also? is it correct? i need am, pm format check please can anyone help me?

How to check time conditionin Jquery

 var startTime="20:02:55"; // or 12:34
 var endTime ="21:02:55"  // or 1:34 
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();

if(time >startTime || time < endTiime){
 $("#a").html("show Box");
}else{
 $("#a").html("Expire BOx");
}

How to check 12 hour and 24 hour condition also? is it correct? i need am, pm format check please can anyone help me?

Share Improve this question edited Apr 9, 2015 at 7:25 Angu asked Apr 9, 2015 at 7:15 AnguAngu 8722 gold badges13 silver badges34 bronze badges 6
  • var hours = date.getHours(); var ampm = hours >= 12 ? 'pm' : 'am'; – Saty Commented Apr 9, 2015 at 7:16
  • 3 You need to also convert startTime and endTime to Date objects so that they can be easily pared. At the moment you're paring a Date to a string which is going to give odd results. – Rory McCrossan Commented Apr 9, 2015 at 7:18
  • Rory is correct. I'd remend momentjs for parsing time strings like that. – CodingIntrigue Commented Apr 9, 2015 at 7:19
  • @Rory McCrossan please can you answer me the convert starttime and endtime to date? – Angu Commented Apr 9, 2015 at 7:22
  • How to check 12 hour and 24 hour also? – Angu Commented Apr 9, 2015 at 7:23
 |  Show 1 more ment

2 Answers 2

Reset to default 2

Here is some code. I am appending to show both behaviour. Here is DEMO

test("20:02:55", "21:02:55");
test("13:02:55", "15:02:55");

function test(start_time, end_time) {
    var dt = new Date();

    //convert both time into timestamp
    var stt = new Date((dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear() + " " + start_time);

    stt = stt.getTime();
    var endt = new Date((dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear() + " " + end_time);
    endt = endt.getTime();

    var time = dt.getTime();

    if (time > stt && time < endt) {
        $("#a").append("<br> show Box ");

    } else {
        $("#a").append("<br> Expire BOx ");

    }
}

Try this. I took the logic to print 'Show Box' if the current time is in between the start and end time. else viceversa.

var startTime="20:02:55"; // or 12:34
 var endTime ="21:02:55"  // or 1:34 
var dt = new Date();
var st = new Date('00','00','00',startTime.split(':')[0],startTime.split(':')[1],startTime.split(':')[2]);
var et = new Date('00','00','00',endTime.split(':')[0],endTime.split(':')[1],endTime.split(':')[2]);
if(dt.getTime() >st.getTime() && dt.getTime() < et.getTime()){
alert("show Box");
}else{
alert("Expire BOx");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745382515a4625293.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信