Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use alongside moment-timezone? #5

Open
rafakato opened this issue May 7, 2015 · 13 comments
Open

How to use alongside moment-timezone? #5

rafakato opened this issue May 7, 2015 · 13 comments

Comments

@rafakato
Copy link

rafakato commented May 7, 2015

I tried to use your plugin with moment-timezone, but it didn't worked.
Have you ever tried?

Thanks

@lennym
Copy link
Owner

lennym commented May 19, 2015

Thanks for raising an issue @rafakato

I haven't tried using alongside moment-timezone. Could you possibly provide more information about what you're trying to do and how exactly it's not working?

@rafakato
Copy link
Author

Hi @lennym,

I was using the moment-timezone in one of my projects because I need to display the datetime in a certain timezone, and their documentation it says to use like this:

var moment = require('moment-timezone');
moment().tz("America/Los_Angeles").format();

But when I try to load your plugin it does not extends the moment()...

For now I removed the moment-timezone in the module I'll use your plugin, but if the two works together it will be best for me.

@lennym
Copy link
Owner

lennym commented May 19, 2015

I just tried running this:

var moment = require('moment-business-time');
require('moment-timezone');

var m = moment('2015-05-19T12:00:00Z').tz('Australia/Sydney').addWorkingTime(5, 'days');

console.log(m.format('DD/MM/YYYY ha z'));

and it seemed to work mostly ok.

There does seem to be a bug with detecting if a current time is a "working time" when the timezone has been set though (in particular, it tests the UTC time and not the local time). I shall look into that.

@rafakato
Copy link
Author

In my application I load the moment-timezone first and expose it in a module, and after I load the moment-business-time where I need it.

Can you check if this works too?

@lennym
Copy link
Owner

lennym commented May 19, 2015

Since both my module, and moment-timezone modify the underlying moment object, then as long as require.resolve resolves to the same instance of moment in each case then everything should work fine.

If it is not working then it's likely a result of your install paths for each module. Try installing moment as a top-level dependency of your project, and running npm dedupe.

@rafakato
Copy link
Author

I have this module that exposes the moment-timezone:

'use strict';

const moment = require('moment-timezone');

module.exports = function (value, tz) {
    if (tz === undefined) {
        tz = 'America/Sao_Paulo';
    }
    if (tz) {
        return moment(value).tz(tz);
    } else {
        return moment(value);
    }
};

@lennym
Copy link
Owner

lennym commented May 19, 2015

I see no particular reason that should cause any problem, aside from possibly the const. Have you tried installing moment as a top-level dependency?

@rafakato
Copy link
Author

I'll try to install it as a top-level dependency. The const is no problem, I use iojs in this project :)

@milovanderlinden
Copy link

I was wondering the same thing. I decided to fork the repository and replace the dependency on moment with moment-timezone. Besides this, I updated dependencies to the latest and add a checkZone that sets the timezone to UTC when no tz.defaultZone is set. My fork is here: https://github.com/codeforeurope/moment-business-time/

@primaryobjects
Copy link

primaryobjects commented Jul 21, 2017

For a specific time-zone, you could use the following:

var date = new Date(new Date().toLocaleString('en-US', { timeZone: 'America/New_York' }));
var result = moment(date).isWorkingTime();

@leoelcoder
Copy link

Time ago I had the same problem, how to use tz with moment-business-time to get time differences. I found this solution for now

first you need to get the offset timezone

offset = moment.tz.zone(timezone).parse();

timezone could be 'America/Bogota' or whatever

So when you are going to use moment-business-time, use in other variable

var business = require('moment-business-time');

add configuration to your business object

then create your moments objects and add the offset

let now = business().utcOffset(offset);
let stateDate =  business(date).utcOffset(offset);

finally use 'moment-business-time' methods normally

@alexander-mart
Copy link

There does seem to be a bug with detecting if a current time is a "working time" when the timezone has been set though (in particular, it tests the UTC time and not the local time). I shall look into that.

Still not working :)
@lennym Did you see this code?

The proposed fork does not solve the problem?
#5 (comment)

@alexander-mart
Copy link

alexander-mart commented Jul 10, 2022

For now, my temporary solution is use self-written function:

const fix_timezone = (datetime, time_zone) => {

  const offset = moment_tz.tz.zone(time_zone).parse();
  // console.log(offset)

  return moment(datetime.utcOffset(-offset)).add(offset, 'minutes')
}

Warning! It was not tested for all cases, but for one timezone :)

Code example:
https://gist.github.com/alexander-mart/63961fbcc19f7d3208b01dac6dbd17f4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants