0

I am using https://valor-software.com/ngx-bootstrap/#/timepicker in my angular project.

I have one task o covert that time to UTC before I request to server.

When I select Time from timepicker value coming is as below.

enter image description here

Now, I have created one function to convert it to UTC using moment.js

static FormatimeSpanBeforeSubmit(date: Date | string ) {

        const dateFromTimePicker = date as Date;


        // Convert it to format "HH:mm:ss"
        let formatedTime = moment.utc(dateFromTimePicker).format('HH:mm:ss');

        // Split details in array because I want seconds as always "00"
        let list = formatedTime.split(/[\s:]+/);

        // Updating last value ""ss" to "00"
        formatedTime = formatedTime.replace(new RegExp(list[list.length -1] + '$'), '00');

        //Final value
        return date ? formatedTime  : null;

    }

Expected Output : It should convert my time as "9:51:00"

Current Output : "10:54:00"

Please help me and guide me how can I get correct value.

Comment :

(2) As per comments from Hoài Nam I have updated my code like below and in that I am still getting 10:54:00

Please find below two images enter image description here

enter image description here

  • Possible duplicate of [How do you convert a JavaScript date to UTC?](https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – Nam Tang Sep 24 '19 at 10:25

2 Answers2

0

i can't comment so i will reply you at here: i found another solution at here https://stackoverflow.com/a/40381090/9768008

this is what i tried:

    var time = 1569326290681; //Tue Sep 24 2019 18:58:10 GMT+0700 (Indochina Time)
    var os = new Date().getTimezoneOffset();
    var converted = new Date((time + (os * 60 * 1000)));
    console.log(converted); // Tue Sep 24 2019 11:58:10 GMT+0700 (Indochina Time)

enter image description here

Nam Tang
  • 100
  • 1
  • 10
0

Issue was date format coming from server was wrong. So, I was getting GMT+ 6.55 instead of GMT+ 8.

Thanks for helping. Addressed both solution. Mine as well as of @HoàiNam are correct.