0

Hi i am working with node.js and kue , i want to delay a job to a future time , what i do is i pass a js date object like this

var today=new Date();
today.setHours(23,00,00);

var email = queue.create('email', {
    title: 'Account renewal required'
  , to: 'tj@learnboost.com'
  , template: 'renewal-email'
}).delay(today)
  .priority('high')
  .save();

This works well but the job triggers at 7.00 am , that is because my timeZone is UTC + 8 , so the default timezone for the date is UTC .

so

I tried to use moment-timezone module for Node.js and doing something like this .

var today = moment().tz("Asia/Hong_Kong").toDate();
today.setHours(23,00,00);

    var email = queue.create('email', {
        title: 'Account renewal required'
      , to: 'tj@learnboost.com'
      , template: 'renewal-email'
    }).delay(today)
      .priority('high')
      .save();

But i found when you use toDate() it ignores the timezone , so i am stuck here , i searched a lot for 2 days and i couldn't find a solution .

Is there any ways to achieve this , please help me , thanks in advance .

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • What you actually want is to parse `23:00` using that timezone, not setting it on the date object. – Bergi Mar 30 '16 at 03:49
  • i think so , is there any way to do it ? – Kanishka Panamaldeniya Mar 30 '16 at 03:52
  • 1
    Presumably it is executing at 07:00 the following day. So adjust the time by your time zone offset: `today.setMinutes(today.getMinutes() + today.getTimezoneOffset())` will subtract 8hrs (javascript timezones are -ve for +UTC) from the Date so it should run at the expected time (and don't use moment.js). – RobG Mar 30 '16 at 03:59
  • 1
    @RobG: I don't think that will change anything, it sounds like the server runs with UTC as the local timezone already. So `today.setHours(23)` and `today.setUTCHours(23)` would have the same effect (and `….getTimezoneOffset()` would be 0). – Bergi Mar 30 '16 at 04:05
  • 1
    Btw, you will need to consider the case that 23:00 is in the past. What should happen then? – Bergi Mar 30 '16 at 04:33
  • for now what i have done is , var today = new Date();today.setUTCHours(15,00,00); , so i will be run on 23:00 Hk Time is this OK ? – Kanishka Panamaldeniya Mar 30 '16 at 04:35
  • @Bergi I have added an answer please review and give your suggestions , thanks – Kanishka Panamaldeniya Mar 30 '16 at 05:01
  • @Bergi—yes, that seems correct. So the OP should set the appropriate UTC time, which should be 23 - 8, or 15:00UTC. – RobG Mar 30 '16 at 05:45

2 Answers2

1

I think you've answered your question, but just to be clear: your host is running on UTC with zero offset, your local time is UTC+08:00 and you want a date for the next time it will be 23:00 local.

In that case, you want a date for 15:00Z. So if the current UTC hour is less than 15, set it to 15. Otherwise, set it to 24 + 15 so that it's 15:00 tomorrow, e.g.

var now = new Date();
now.setHours((now.getHours() < 15? 15 : 39), 0,0,0);

That way you do the date and time in one go. But this assumes that the host is set to UTC.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • 1
    You should use `…UTCHours`, so you don't have to assume anything and it just works as expected. – Bergi Mar 30 '16 at 14:00
0

Hi i came up a solution like this , please review it and give your suggestions .

            var today = new Date();
            var hours = 15;

            if(today.getUTCHours >= hours ) {
                today.setUTCDate(today.getUTCDate()+1);
            }
            today.setUTCHours(hours,00,00);

I will check the hour and if it is already passed 11.00 pm HK time , i will change the date to the next day .

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • 1
    That looks good, yes. Instead of using `UTCDate`, you can also do `hours += 24`. And you might want to add a comment like `// 23 o'Clock in Asia/Hong Kong` to the `15`. – Bergi Mar 30 '16 at 13:58
  • 1
    However, you'll need to be aware that this does not respect daylight saving times in your chosen timezone. Not sure whether you have one, or whether it would be expected. – Bergi Mar 30 '16 at 13:59