I want to return a string that is grammatically correct to display the current uptime. For example, 3 years, 7 months, 11 days, 1 hour, 16 minutes and zero seconds, which means that single units should not be multiple, and zero units should be multiple, although zero should not be displayed if it has not yet appeared ( for example, do not show years, months, etc., if this has not happened yet)
Since the ticks method will not work for more than a month, I use ManagementObject, but I am confused about how to perform datetime calculations and break (I am very new to C #, so I am making a utility that does many functions, so I can learn many things.
this is what I have now, and it’s not very much ...
any help is appreciated.
public string getUptime()
{
SelectQuery query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary='true'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get())
{
DateTime boot = ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
}
string now = DateTime.Now.ToShortDateString();
return "3 years, 7 months, 11 days, 1 hour, 16 minutes and zero seconds";
}
source
share