Hi,
How to add 6 hours in time format ? 10:17 ====>this is my time how to add 6 hrs in this format
Thanks in advance
Hi,
How to add 6 hours in time format ? 10:17 ====>this is my time how to add 6 hrs in this format
Thanks in advance
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Mar 15, 2023, 6:17 AM EDT
Hello Vinot
Please check the below code to add hours in time.
DateTime dateTime = DateTime.ParseExact("10:17", "HH:mm",CultureInfo.InvariantCulture); dateTime = dateTime.AddHours(6);Thanks
Vishal
Hiten Pingolia151 reputationPosted Mar 15, 2023, 8:00 AM EDT
JavaScript using the
Dateobject and itssetHours()method.// Create a new Date object var myDate = new Date(); // Add 2 hours to the current time myDate.setHours(myDate.getHours() + 2); // Convert the Date object to a string in time format var timeString = myDate.toLocaleTimeString(); // Output the time string console.log(timeString);Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 16, 2023, 12:16 AM EDT
Hello Vinoth,
In angular, you can do like following:
let timeStr = "10:17"; let time = new Date(); time.setHours(Number(timeStr.substr(0, 2)) + 6); time.setMinutes(Number(timeStr.substr(3))); let updatedTimeStr = `${time.getHours().toString().padStart(2, '0')}:${time.getMinutes().toString().padStart(2, '0')}`; console.log(updatedTimeStr) //16:17Thanks
Naimish Makwana
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 15, 2023, 7:04 AM EDT
Hello Kumar,
To add 6 hours to a given time in the format of "hh:mm", you can use the following steps:
1. Convert the time string to a DateTime object in C#.
string time = "10:17"; DateTime datetime = DateTime.ParseExact(time, "HH:mm", CultureInfo.InvariantCulture);2. Add 6 hours to the DateTime object using the AddHours() method.
datetime = datetime.AddHours(6);3. Convert the DateTime object back to a string in the original format.
string newTime = datetime.ToString("HH:mm");The final code would look like this:
string time = "10:17"; DateTime datetime = DateTime.ParseExact(time, "HH:mm", CultureInfo.InvariantCulture); datetime = datetime.AddHours(6); string newTime = datetime.ToString("HH:mm"); Console.WriteLine(newTime);This will output the new time with 6 hours added in the format of "hh:mm". In this case, the output will be "16:17".