Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Convert String time into real time

Hey guys! I am stuggling to learn one things, here is two string time as below:

'00:00:00' and '01:00:00'.

I want to calculate this two time. Maintain time or different time. I dont know how to do it.

I know i should convert them to number first, then minus them together.

So, the problem is i know the progress, but i dont know how to do.

Hope someone can help me out and thank you so much!!!

1 Answer

Steven Parker
Steven Parker
231,171 Points

JavaScript has a "Date" object that also contains time. You can convert to that representation, and then a difference will be in the number of milliseconds. You can also convert the difference back into another date object and then use formatting to display it as a time:

var t1 = new Date('0 00:00:00');  // use 0 for date part since we only care about time
var t2 = new Date('0 01:00:00');

console.log(t2 - t1);  // 3600000 (difference in milliseconds)

var diff = new Date(t2 - t1);
console.log(diff.toLocaleTimeString('en-US', {timeZone: "UTC", hour12: false}));  // 01:00:00

For more details, see the MDN pages beginning with Date.

Thank you so much!!!! You really help me a lot!

Hi, last time you really helped me a lot. Can I just ask one more question?

The question is I want to convert '01:00:00' to '1 hour'. And if the result is '01:05:00', then I want to convert '1 hours 5 minutes'. If the result is '02:08:00', then the result is '2hours 8minutes.'

Thank you for helping me last time gain, thank you so much!

Steven Parker
Steven Parker
231,171 Points

Check those MDN pages, the "Date" object has methods like "getHours" and "getMinutes" that will help you create the kind of strings you want.

Thank you for the reply again! I do tried getHours() or getMinutes() from the diff variable. But it shows getHours is not a function of diff variable....

Steven Parker
Steven Parker
231,171 Points

Works for me. But if you're using it on "diff" you want "getUTCHours" anyway:

var t1 = new Date('0 00:00:00');
var t2 = new Date('0 01:00:00');
var diff = new Date(t2 - t1);
console.log(diff.getHours());     // 19 <-- for my time zone, yours may differ
console.log(diff.getUTCHours());  // 1

Thank you so much again!! I totally missed that, because I just directly add the getHours to ''diff.toLocaleTimeString('en-US', {timeZone: "UTC", hour12: false})''. Thats why I got error.

Thank you so much!! You help me a lot!!!!

By the way, I just want to know how do you train your programming logic? I know I learned a lot from the Treehouse, but if I want to upgrade the logic of programming, how do I achieve that? And do you have any resource to train yourself?

I am really appreciated, you really help me a lot. Thank you!!

Steven Parker
Steven Parker
231,171 Points

It's mostly experience. I've been a developer for several decades. :wink:
Programming puzzles like Advent of Code can help sharpen your skills.

Oh, ok!! Thank you for helping me, I 'm really appreciated. Thank you so much!