Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

UTC to local time #114

Closed
Hesesses opened this issue Mar 3, 2015 · 10 comments
Closed

UTC to local time #114

Hesesses opened this issue Mar 3, 2015 · 10 comments
Assignees
Milestone

Comments

@Hesesses
Copy link

Hesesses commented Mar 3, 2015

My app gets utc datetime value from backend ('2015-03-03 11:18:33' for example)

How to convert this to user's local time?

@pvantrepote
Copy link

I have the same issue, I can't find a way to call .local() after utc().
Idea, change the preprocess config to be an array.
preprocess = ['utc', 'local']

@pvantrepote
Copy link

I did a quick test, and this is working:

            this.preprocessors = {
                utc: moment.utc,
                local: function(aMoment) {
                    if (!moment.isMoment(aMoment)) {
                        $log.warn('angular-moment: Ignoring unsupported value for local preprocess: ' + aMoment);
                        return aMoment;
                    }

                    return aMoment.local();
                },
                unix: moment.unix
            };

            this.preprocessDate = function (value, preprocess, format) {
                if (angular.isUndefined(preprocess)) {
                    preprocess = angularMomentConfig.preprocess;
                }

                if (preprocess instanceof Array) {
                    var previousResult = value;
                    var $this = this;

                    preprocess.forEach(function(pre) {
                        previousResult = $this.preprocessDate(previousResult, pre, format);
                    });

                    return previousResult;
                }

                if (this.preprocessors[preprocess]) {
                    return this.preprocessors[preprocess](value, format);
                }
                if (preprocess) {
                    $log.warn('angular-moment: Ignoring unsupported value for preprocess: ' + preprocess);
                }
                if (!isNaN(parseFloat(value)) && isFinite(value)) {
                    // Milliseconds since the epoch
                    return moment(parseInt(value, 10));
                }
                // else just returns the value as-is.
                return moment(value, format);
            };

`

@circuitrider
Copy link

What is "this" in this context for your snippet?

@jwohlfert23
Copy link

+1

@pvantrepote
Copy link

Sorry for the delay, just got an email this morning.

this is the amMoment service.
If you want to try it, just replace those 2 methods in the amMoment service by the one in the snippet.

@staff0rd
Copy link

staff0rd commented Aug 7, 2015

this worked great, thanks!

@dibayendu
Copy link

This has worked for me:

(function() {
  'use strict'

  angular
    .module('myApp')
    .run(function (moment, amMoment) {
      moment.tz.setDefault("utc");  // make sure to include moment-timezone.js
      amMoment.changeTimezone('Australia/Melbourne');
    });
})();

@ghost
Copy link

ghost commented Sep 12, 2015

I also have this issue. In my case the timestamp is saved as UTC on the server, while the users should view the date in their own local time without explicitly declaring it. Modifying the service is a no-go as future bower updates will destroy the edits.

@urish urish added this to the 1.0.0 milestone Sep 15, 2015
@urish
Copy link
Owner

urish commented Sep 15, 2015

We will add a amLocal filter to 1.0.0

@urish urish self-assigned this Sep 15, 2015
@urish urish closed this as completed in 0d2b1a9 Sep 17, 2015
@ghost
Copy link

ghost commented Sep 18, 2015

nice

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

No branches or pull requests

7 participants