Converting java calendar date to javascript date using new Date().getTimezoneOffset()












0















I have a calendar data that is send to the server, it contains this fields-



export interface CalendarDate{
dayOfMonth: number;
hourOfDay: number;
minute: number;
month: number;
second: number;
year: number;
}


I want to convert it to javascript Date with reference to the time offset calculation, I read a lot of posts in this matter, is it correct to do it like this? will it work in all cases?



 return new Date(modificationDate.year, modificationDate.month, modificationDate.dayOfMonth,
modificationDate.hourOfDay, modificationDate.minute -new Date().getTimezoneOffset(), modificationDate.second);









share|improve this question























  • What is your definition of "work"? Why not just use Date.UTC and ignore the timezone?

    – RobG
    Nov 14 '18 at 20:51











  • What do you mean? how can I use I need a date object and not a number.

    – danda
    Nov 15 '18 at 4:55











  • I need the actual local date for display issues in UI.

    – danda
    Nov 15 '18 at 5:02











  • What your code appears to be doing is treating UTC values as local, so you then subtract the timezone offset to get back to the correct time. If you use Date.UTC you avoid that. Provide some example input and output per How to create a minimal, complete and verifiable example.

    – RobG
    Nov 15 '18 at 6:07


















0















I have a calendar data that is send to the server, it contains this fields-



export interface CalendarDate{
dayOfMonth: number;
hourOfDay: number;
minute: number;
month: number;
second: number;
year: number;
}


I want to convert it to javascript Date with reference to the time offset calculation, I read a lot of posts in this matter, is it correct to do it like this? will it work in all cases?



 return new Date(modificationDate.year, modificationDate.month, modificationDate.dayOfMonth,
modificationDate.hourOfDay, modificationDate.minute -new Date().getTimezoneOffset(), modificationDate.second);









share|improve this question























  • What is your definition of "work"? Why not just use Date.UTC and ignore the timezone?

    – RobG
    Nov 14 '18 at 20:51











  • What do you mean? how can I use I need a date object and not a number.

    – danda
    Nov 15 '18 at 4:55











  • I need the actual local date for display issues in UI.

    – danda
    Nov 15 '18 at 5:02











  • What your code appears to be doing is treating UTC values as local, so you then subtract the timezone offset to get back to the correct time. If you use Date.UTC you avoid that. Provide some example input and output per How to create a minimal, complete and verifiable example.

    – RobG
    Nov 15 '18 at 6:07
















0












0








0








I have a calendar data that is send to the server, it contains this fields-



export interface CalendarDate{
dayOfMonth: number;
hourOfDay: number;
minute: number;
month: number;
second: number;
year: number;
}


I want to convert it to javascript Date with reference to the time offset calculation, I read a lot of posts in this matter, is it correct to do it like this? will it work in all cases?



 return new Date(modificationDate.year, modificationDate.month, modificationDate.dayOfMonth,
modificationDate.hourOfDay, modificationDate.minute -new Date().getTimezoneOffset(), modificationDate.second);









share|improve this question














I have a calendar data that is send to the server, it contains this fields-



export interface CalendarDate{
dayOfMonth: number;
hourOfDay: number;
minute: number;
month: number;
second: number;
year: number;
}


I want to convert it to javascript Date with reference to the time offset calculation, I read a lot of posts in this matter, is it correct to do it like this? will it work in all cases?



 return new Date(modificationDate.year, modificationDate.month, modificationDate.dayOfMonth,
modificationDate.hourOfDay, modificationDate.minute -new Date().getTimezoneOffset(), modificationDate.second);






javascript angular typescript date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 12:29









dandadanda

477




477













  • What is your definition of "work"? Why not just use Date.UTC and ignore the timezone?

    – RobG
    Nov 14 '18 at 20:51











  • What do you mean? how can I use I need a date object and not a number.

    – danda
    Nov 15 '18 at 4:55











  • I need the actual local date for display issues in UI.

    – danda
    Nov 15 '18 at 5:02











  • What your code appears to be doing is treating UTC values as local, so you then subtract the timezone offset to get back to the correct time. If you use Date.UTC you avoid that. Provide some example input and output per How to create a minimal, complete and verifiable example.

    – RobG
    Nov 15 '18 at 6:07





















  • What is your definition of "work"? Why not just use Date.UTC and ignore the timezone?

    – RobG
    Nov 14 '18 at 20:51











  • What do you mean? how can I use I need a date object and not a number.

    – danda
    Nov 15 '18 at 4:55











  • I need the actual local date for display issues in UI.

    – danda
    Nov 15 '18 at 5:02











  • What your code appears to be doing is treating UTC values as local, so you then subtract the timezone offset to get back to the correct time. If you use Date.UTC you avoid that. Provide some example input and output per How to create a minimal, complete and verifiable example.

    – RobG
    Nov 15 '18 at 6:07



















What is your definition of "work"? Why not just use Date.UTC and ignore the timezone?

– RobG
Nov 14 '18 at 20:51





What is your definition of "work"? Why not just use Date.UTC and ignore the timezone?

– RobG
Nov 14 '18 at 20:51













What do you mean? how can I use I need a date object and not a number.

– danda
Nov 15 '18 at 4:55





What do you mean? how can I use I need a date object and not a number.

– danda
Nov 15 '18 at 4:55













I need the actual local date for display issues in UI.

– danda
Nov 15 '18 at 5:02





I need the actual local date for display issues in UI.

– danda
Nov 15 '18 at 5:02













What your code appears to be doing is treating UTC values as local, so you then subtract the timezone offset to get back to the correct time. If you use Date.UTC you avoid that. Provide some example input and output per How to create a minimal, complete and verifiable example.

– RobG
Nov 15 '18 at 6:07







What your code appears to be doing is treating UTC values as local, so you then subtract the timezone offset to get back to the correct time. If you use Date.UTC you avoid that. Provide some example input and output per How to create a minimal, complete and verifiable example.

– RobG
Nov 15 '18 at 6:07














1 Answer
1






active

oldest

votes


















1














What you appear to be doing is using UTC values as local, so you then have to use the timezone offset to get back to the correct time.



No, it will not work in all cases as you are getting the timezone offset for when the function is run, not for the date you're parsing. That may differ in places where the timezone offset has changed either through government regulation or legislation, or from daylight saving. E.g. the Line Islands recently changed from -12 to +14.



So assuming the data you get is UTC, you can use Date.UTC to get the correct date and time:






// data for 2018-01-01T12:00:00Z
var data = {
dayOfMonth: 1,
hourOfDay: 12,
minute: 0,
month: 0,
second: 0,
year: 2018
};

// Current function, uses current timezone offset regardless of date
function toDate(data) {
return new Date(
data.year, data.month, data.dayOfMonth, data.hourOfDay,
data.minute - new Date().getTimezoneOffset(), data.second);
}

// Better function, pure UTC
function toDateNew(data) {
return new Date(Date.UTC(
data.year, data.month, data.dayOfMonth, data.hourOfDay,
data.minute, data.second));
}

console.log('Existing: ' + toDate(data).toISOString() +
'nor : ' + toDate(data).toString() +
'nNew func: ' + toDateNew(data).toISOString() +
'nor : ' + toDateNew(data).toString()
);





So now the values are parsed as UTC, setting the date's time value to the correct value. When you display the date, local values will be used that will adjust for the timezone offset for the date based on host system settings. You don't have to do anything.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300278%2fconverting-java-calendar-date-to-javascript-date-using-new-date-gettimezoneoff%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    What you appear to be doing is using UTC values as local, so you then have to use the timezone offset to get back to the correct time.



    No, it will not work in all cases as you are getting the timezone offset for when the function is run, not for the date you're parsing. That may differ in places where the timezone offset has changed either through government regulation or legislation, or from daylight saving. E.g. the Line Islands recently changed from -12 to +14.



    So assuming the data you get is UTC, you can use Date.UTC to get the correct date and time:






    // data for 2018-01-01T12:00:00Z
    var data = {
    dayOfMonth: 1,
    hourOfDay: 12,
    minute: 0,
    month: 0,
    second: 0,
    year: 2018
    };

    // Current function, uses current timezone offset regardless of date
    function toDate(data) {
    return new Date(
    data.year, data.month, data.dayOfMonth, data.hourOfDay,
    data.minute - new Date().getTimezoneOffset(), data.second);
    }

    // Better function, pure UTC
    function toDateNew(data) {
    return new Date(Date.UTC(
    data.year, data.month, data.dayOfMonth, data.hourOfDay,
    data.minute, data.second));
    }

    console.log('Existing: ' + toDate(data).toISOString() +
    'nor : ' + toDate(data).toString() +
    'nNew func: ' + toDateNew(data).toISOString() +
    'nor : ' + toDateNew(data).toString()
    );





    So now the values are parsed as UTC, setting the date's time value to the correct value. When you display the date, local values will be used that will adjust for the timezone offset for the date based on host system settings. You don't have to do anything.






    share|improve this answer






























      1














      What you appear to be doing is using UTC values as local, so you then have to use the timezone offset to get back to the correct time.



      No, it will not work in all cases as you are getting the timezone offset for when the function is run, not for the date you're parsing. That may differ in places where the timezone offset has changed either through government regulation or legislation, or from daylight saving. E.g. the Line Islands recently changed from -12 to +14.



      So assuming the data you get is UTC, you can use Date.UTC to get the correct date and time:






      // data for 2018-01-01T12:00:00Z
      var data = {
      dayOfMonth: 1,
      hourOfDay: 12,
      minute: 0,
      month: 0,
      second: 0,
      year: 2018
      };

      // Current function, uses current timezone offset regardless of date
      function toDate(data) {
      return new Date(
      data.year, data.month, data.dayOfMonth, data.hourOfDay,
      data.minute - new Date().getTimezoneOffset(), data.second);
      }

      // Better function, pure UTC
      function toDateNew(data) {
      return new Date(Date.UTC(
      data.year, data.month, data.dayOfMonth, data.hourOfDay,
      data.minute, data.second));
      }

      console.log('Existing: ' + toDate(data).toISOString() +
      'nor : ' + toDate(data).toString() +
      'nNew func: ' + toDateNew(data).toISOString() +
      'nor : ' + toDateNew(data).toString()
      );





      So now the values are parsed as UTC, setting the date's time value to the correct value. When you display the date, local values will be used that will adjust for the timezone offset for the date based on host system settings. You don't have to do anything.






      share|improve this answer




























        1












        1








        1







        What you appear to be doing is using UTC values as local, so you then have to use the timezone offset to get back to the correct time.



        No, it will not work in all cases as you are getting the timezone offset for when the function is run, not for the date you're parsing. That may differ in places where the timezone offset has changed either through government regulation or legislation, or from daylight saving. E.g. the Line Islands recently changed from -12 to +14.



        So assuming the data you get is UTC, you can use Date.UTC to get the correct date and time:






        // data for 2018-01-01T12:00:00Z
        var data = {
        dayOfMonth: 1,
        hourOfDay: 12,
        minute: 0,
        month: 0,
        second: 0,
        year: 2018
        };

        // Current function, uses current timezone offset regardless of date
        function toDate(data) {
        return new Date(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute - new Date().getTimezoneOffset(), data.second);
        }

        // Better function, pure UTC
        function toDateNew(data) {
        return new Date(Date.UTC(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute, data.second));
        }

        console.log('Existing: ' + toDate(data).toISOString() +
        'nor : ' + toDate(data).toString() +
        'nNew func: ' + toDateNew(data).toISOString() +
        'nor : ' + toDateNew(data).toString()
        );





        So now the values are parsed as UTC, setting the date's time value to the correct value. When you display the date, local values will be used that will adjust for the timezone offset for the date based on host system settings. You don't have to do anything.






        share|improve this answer















        What you appear to be doing is using UTC values as local, so you then have to use the timezone offset to get back to the correct time.



        No, it will not work in all cases as you are getting the timezone offset for when the function is run, not for the date you're parsing. That may differ in places where the timezone offset has changed either through government regulation or legislation, or from daylight saving. E.g. the Line Islands recently changed from -12 to +14.



        So assuming the data you get is UTC, you can use Date.UTC to get the correct date and time:






        // data for 2018-01-01T12:00:00Z
        var data = {
        dayOfMonth: 1,
        hourOfDay: 12,
        minute: 0,
        month: 0,
        second: 0,
        year: 2018
        };

        // Current function, uses current timezone offset regardless of date
        function toDate(data) {
        return new Date(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute - new Date().getTimezoneOffset(), data.second);
        }

        // Better function, pure UTC
        function toDateNew(data) {
        return new Date(Date.UTC(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute, data.second));
        }

        console.log('Existing: ' + toDate(data).toISOString() +
        'nor : ' + toDate(data).toString() +
        'nNew func: ' + toDateNew(data).toISOString() +
        'nor : ' + toDateNew(data).toString()
        );





        So now the values are parsed as UTC, setting the date's time value to the correct value. When you display the date, local values will be used that will adjust for the timezone offset for the date based on host system settings. You don't have to do anything.






        // data for 2018-01-01T12:00:00Z
        var data = {
        dayOfMonth: 1,
        hourOfDay: 12,
        minute: 0,
        month: 0,
        second: 0,
        year: 2018
        };

        // Current function, uses current timezone offset regardless of date
        function toDate(data) {
        return new Date(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute - new Date().getTimezoneOffset(), data.second);
        }

        // Better function, pure UTC
        function toDateNew(data) {
        return new Date(Date.UTC(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute, data.second));
        }

        console.log('Existing: ' + toDate(data).toISOString() +
        'nor : ' + toDate(data).toString() +
        'nNew func: ' + toDateNew(data).toISOString() +
        'nor : ' + toDateNew(data).toString()
        );





        // data for 2018-01-01T12:00:00Z
        var data = {
        dayOfMonth: 1,
        hourOfDay: 12,
        minute: 0,
        month: 0,
        second: 0,
        year: 2018
        };

        // Current function, uses current timezone offset regardless of date
        function toDate(data) {
        return new Date(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute - new Date().getTimezoneOffset(), data.second);
        }

        // Better function, pure UTC
        function toDateNew(data) {
        return new Date(Date.UTC(
        data.year, data.month, data.dayOfMonth, data.hourOfDay,
        data.minute, data.second));
        }

        console.log('Existing: ' + toDate(data).toISOString() +
        'nor : ' + toDate(data).toString() +
        'nNew func: ' + toDateNew(data).toISOString() +
        'nor : ' + toDateNew(data).toString()
        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 6:28

























        answered Nov 15 '18 at 6:21









        RobGRobG

        98.1k19106145




        98.1k19106145






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300278%2fconverting-java-calendar-date-to-javascript-date-using-new-date-gettimezoneoff%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python