Having problems with datetime

i have to compare stuff the hour before my program run, but i need time to be 07:00 and not 7:00
also after noon it needs to be 13:00 and not 1:00.
It always need to be the complete hour, so i’ve made it like this variable

lasthour = Datetime.Now.AddHours(-1).TimeOfDay.Hours.ToString+“:00”

but that gives me 7:00
is there a way to turn that into 07:00, but still keep military time format?

thanks

@Jakob_nesnes

Try this:

 DateTime.Now.AddHours(-1).Tostring("HH:mm")
2 Likes

@Jakob_nesnes,

Try like this,

Datetime.Now.AddHours(-1).ToString("HH:mm")
1 Like

You can use PadLeft to add zeroes.

Datetime.Now.AddHours(-1).TimeOfDay.Hours.ToString.PadLeft(2, '0') 
1 Like

DateTime.Now.AddHours(-1).ToString(“HH:00”)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.