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

Local Date #10

Closed
wants to merge 2 commits into from
Closed

Local Date #10

wants to merge 2 commits into from

Conversation

vladjerca
Copy link

I'm currently working on a project and found your library. It's light weight and does exactly what I need, it lacked the local ISO string format convenience method which I personally need in my current project.

I added the 'getLocalDate' to allow date fetching with the client offset.

Original source:
http://stackoverflow.com/questions/17415579/how-to-iso-8601-format-a-date-with-timezone-offset-in-javascript

Added 'getLocalDate' to allow date fetching with the client offset.
Added optional timezone override.
@eight04
Copy link
Owner

eight04 commented Nov 7, 2015

How about to move this method into a service instead of adding it to this module? It doesn't require Angular or the datetime parser.

function getLocalISOString(now) {
    var tzo = -now.getTimezoneOffset(),
        dif = tzo >= 0 ? '+' : '-',
        pad = function(num) {
            var norm = Math.abs(Math.floor(num));
            return (norm < 10 ? '0' : '') + norm;
        };
    return now.getFullYear() 
        + '-' + pad(now.getMonth()+1)
        + '-' + pad(now.getDate())
        + 'T' + pad(now.getHours())
        + ':' + pad(now.getMinutes()) 
        + ':' + pad(now.getSeconds()) 
        + dif + pad(tzo / 60) 
        + ':' + pad(tzo % 60);
}

Then use it like:

localDate = getLocalISOString(myDate);

Or, extend the Date class:

Date.prototype.toLocalISOString = function(){
    var now = this,
        tzo = -now.getTimezoneOffset(),
        dif = tzo >= 0 ? '+' : '-',
        pad = function(num) {
            var norm = Math.abs(Math.floor(num));
            return (norm < 10 ? '0' : '') + norm;
        };
    return now.getFullYear()
        + '-' + pad(now.getMonth()+1)
        + '-' + pad(now.getDate())
        + 'T' + pad(now.getHours())
        + ':' + pad(now.getMinutes())
        + ':' + pad(now.getSeconds())
        + dif + pad(tzo / 60)
        + ':' + pad(tzo % 60);
};

Then use it like:

localDate = myDate.toLocalISOString();

@vladjerca
Copy link
Author

No worries, I just thought it would be a helpful extension.

@vladjerca vladjerca closed this Nov 9, 2015
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

Successfully merging this pull request may close these issues.

None yet

2 participants