Home > C# > Convert Milliseconds, Seconds, Minutes, Hours, Days using C#

Convert Milliseconds, Seconds, Minutes, Hours, Days using C#

In our example here, The “TimeConversionUtil” class contains all the required wrapper methods (already provided by the “TimeSpan” class) for converting to and from Milliseconds, Seconds, Minutes, Hours and Days; This helper class gives us the flexibility to convert easily using C# language.

using System;

class TimeConversionUtil
{

    #region Convert To Days

    public static double ConvertMillisecondsToDays(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalDays;
    }

    public static double ConvertSecondsToDays(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalDays;
    }

    public static double ConvertMinutesToDays(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalDays;
    }

    public static double ConvertHoursToDays(double hours)
    {
        return TimeSpan.FromHours(hours).TotalDays;
    }

    #endregion

    #region Convert To Hours

    public static double ConvertMillisecondsToHours(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalHours;
    }

    public static double ConvertSecondsToHours(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalHours;
    }

    public static double ConvertMinutesToHours(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalHours;
    }

    public static double ConvertDaysToHours(double days)
    {
        return TimeSpan.FromDays(days).TotalHours;
    }

    #endregion

    #region Convert To Minutes

    public static double ConvertMillisecondsToMinutes(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes;
    }

    public static double ConvertSecondsToMinutes(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalMinutes;
    }

    public static double ConvertHoursToMinutes(double hours)
    {
        return TimeSpan.FromHours(hours).TotalMinutes;
    }

    public static double ConvertDaysToMinutes(double days)
    {
        return TimeSpan.FromDays(days).TotalMinutes;
    }

    #endregion

    #region Convert To Seconds

    public static double ConvertMillisecondsToSeconds(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds;
    }

    public static double ConvertMinutesToSeconds(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalSeconds;
    }

    public static double ConvertHoursToSeconds(double hours)
    {
        return TimeSpan.FromHours(hours).TotalSeconds;
    }

    public static double ConvertDaysToSeconds(double days)
    {
        return TimeSpan.FromDays(days).TotalSeconds;
    }

    #endregion

    #region Convert To Milliseconds

    public static double ConvertSecondsToMilliseconds(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalMilliseconds;
    }

    public static double ConvertMinutesToMilliseconds(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalMilliseconds;
    }

    public static double ConvertHoursToMilliseconds(double hours)
    {
        return TimeSpan.FromHours(hours).TotalMilliseconds;
    }

    public static double ConvertDaysToMilliseconds(double days)
    {
        return TimeSpan.FromDays(days).TotalMilliseconds;
    }

    #endregion

}

Advertisements
  1. July 9, 2011 at 2:37 PM

    The guidelines you provided listed below are extremely precious. It been found this type of pleasurable surprise to obtain that expecting me when I woke up today. They’re constantly to the issue easy to learn. Thanks a large amount for any valuable ideas you’ve got shared right here.

  2. February 20, 2012 at 2:06 AM

    Hey There. I found your blog using msn. This is an extremely well written article. I’ll be sure to bookmark it and come back to read more of Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support . Thanks for the post. I’ll definitely comeback.

  3. February 21, 2012 at 6:07 AM

    Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support I was recommended this website by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my trouble. You’re amazing! Thanks! your article about Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support Best Regards Shane Cassetta

  4. February 22, 2012 at 3:06 AM

    Hey There. I found your blog using msn. This is a really well written article. I’ll make sure to bookmark it and come back to read more of Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support . Thanks for the post. I will definitely return.

  5. February 22, 2012 at 4:32 AM

    I got what you will, thanks for putting up. Woh I am willing to ascertain this website through google. Thanks For Share Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support.

  6. February 22, 2012 at 5:20 AM

    An fascinating treatment is designer comment. I anticipate that you should pen many on this content, it strength not be a sacred case but mostly grouping are not enough to utter on specified topics. To the succeeding. Cheers like your Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support.

  7. March 5, 2012 at 5:25 AM

    I got what you wish, thanks for swing up. Woh I am willing to bump this website through google. Thanks For Share Convert Milliseconds, Seconds, Minutes, Hours, Days using C# Code Support.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: