简体   繁体   English

如何停止代码在特定日期运行

[英]How to stop code from running on a specific date

I want to stop a specific part of my code from running when its 29th of may .我想在 5 月29 日停止运行我的代码的特定部分。 Someone can help me out on this with jQuery or JavaScript .有人可以用jQueryJavaScript帮助我解决这个问题。

var diff = ( new Date('2016/05/29 00:00:01') - new Date() );
if( diff > 0 ){
   /*   Your Code here  */
}

If the diff is negative then today date exceed the May 29th如果差异为负,则今天的日期超过 5 月 29 日

You can try something like this,你可以试试这样的

var CurrentDate = new Date();
var YourSelectedDate = new Date(2016, 04, 29, 10, 15); //Here it is 29th may 2016

    if(CurrentDate > YourSelectedDate ){
        //CurrentDate is more than YourSelectedDate 
       // Display warning message or whatever you want to display.
    }
    else{
        //YourSelectedDate is more than CurrentDate
        //put your code in this part.
    }

This is an example这是一个例子

You can work with Date objects ;您可以使用Date 对象

Here is something you can start with, there are much better ways to do this, because you didn't show any code i assume you do not know Date Objects, have fun coding!这是您可以开始的东西,有更好的方法可以做到这一点,因为您没有显示任何代码我假设您不知道日期对象,祝您编码愉快!

var randDate = new Date(2016, 04, 29, 10, 15);
randDate.setHours(0);
randDate.setMinutes(0);
randDate.setSeconds(0);
randDate.setMilliseconds(0);

var target = new Date(2016, 04, 29); 

target.setHours(0);
target.setMinutes(0);
target.setSeconds(0);
target.setMilliseconds(0);

console.log(target.getTime() === randDate.getTime());


//output: true

Example II例二

var randDate = new Date(2016, 04, 29, 10, 15);
//randDate is your now() on any day
randDate.setHours(0,0,0,0);
var now = new Date();
now.setHours(0,0,0,0);
//strips h, m, s, ms from var now && var randDate



var target = new Date(2016, 04, 29);
//set up your target
target.setHours(0,0,0,0);
//strips h, m, s, ms if needed


console.log(target.getTime() === randDate.getTime());
//output: true
console.log(target.getTime() === now.getTime());
//output: false (Mon May 09 2016 00:00:00 GMT+0200 (Romance (zomertijd)))

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

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