-1

How to convert milliseconds to a readable time text?

For example : 60000 would be 1 Minute, 60500 would be 1 Minute and 500 Milliseconds, etc.

Edit :

Answered by me in answers.

  • I'm a bit confused: you asked this question and immediately answered to it. What do you expect us to do with the question? – Klaus Gütter Nov 24 '18 at 15:45
  • 1
    I think it's alright for users to ask and answer their own questions they have solutions for if they think it might benefit other users. However in this case I think .NET framework already has convenient display formatting available for times so the usefulness of this particular Q&A is debatable – Wubbler Nov 24 '18 at 15:51
  • @Wubbler yes, it is OK, was just wondering because the answer was posted in the same minute as the question. – Klaus Gütter Nov 24 '18 at 15:56
  • @KlausGütter its a question i was willing to post, tried to solve it myself before so and done it, so posted the "log" if u may say... – Matan Yashar Nov 24 '18 at 16:17
  • Maybe this helps: https://stackoverflow.com/a/56265960/4247806 (duplicate?) – mike May 22 '19 at 22:54

2 Answers2

0

I've created a function that does it, the user can choose how to display the time text.

public class MakeReadableTimeType
{
    public class GetMakeReadableTimeType
    {
        public bool
            DisplayDays
            , DisplayHours
            , DisplayMinutes
            , DisplaySeconds
            , DisplayMilliseconds;

        public long SourceMilliseconds;
    }

    public static string MakeReadableTime(GetMakeReadableTimeType GetMakeReadableTime)
    {
        GetMakeReadableTimeType FunctionGet = GetMakeReadableTime;
        string
            FunctionResult
            , CurrentTimeText = "";

        TimeSpan TimeSpanObject = TimeSpan.FromMilliseconds(FunctionGet.SourceMilliseconds);
        long
            CurrentDays = 0
            , CurrentHours = 0
            , CurrentMinutes = 0
            , CurrentSeconds = 0
            , CurrentMilliseconds = 0;

        List<string> TimeTextList = new List<string>();

        if (TimeSpanObject.Days > 0)
            if (FunctionGet.DisplayDays == true)
                CurrentDays += TimeSpanObject.Days;
            else
            {
                if (FunctionGet.DisplayHours == true)
                    CurrentHours += (long)TimeSpanObject.Days * 24;
                else
                {
                    if (FunctionGet.DisplayMinutes == true)
                        CurrentMinutes += (long)TimeSpanObject.Days * 24 * 60;
                    else
                    {
                        if (FunctionGet.DisplaySeconds == true)
                            CurrentSeconds += (long)TimeSpanObject.Days * 24 * 60 * 60;
                        else
                        {
                            if (FunctionGet.DisplayMilliseconds == true)
                                CurrentMilliseconds += (long)TimeSpanObject.Days * 24 * 60 * 60 * 1000;
                        }
                    }
                }
            }

        if (TimeSpanObject.Hours > 0)
            if (FunctionGet.DisplayHours == true)
                CurrentHours += TimeSpanObject.Hours;
            else
            {
                if (FunctionGet.DisplayMinutes == true)
                    CurrentMinutes += (long)TimeSpanObject.Hours * 60;
                else
                {
                    if (FunctionGet.DisplaySeconds == true)
                        CurrentSeconds += (long)TimeSpanObject.Hours * 60 * 60;
                    else
                    {
                        if (FunctionGet.DisplayMilliseconds == true)
                            CurrentMilliseconds += (long)TimeSpanObject.Hours * 60 * 60 * 1000;
                    }
                }
            }

        if (TimeSpanObject.Minutes > 0)
            if (FunctionGet.DisplayMinutes == true)
                CurrentMinutes += TimeSpanObject.Minutes;
            else
            {
                if (FunctionGet.DisplaySeconds == true)
                    CurrentSeconds += (long)TimeSpanObject.Minutes * 60;
                else
                {
                    if (FunctionGet.DisplayMilliseconds == true)
                        CurrentMilliseconds += (long)TimeSpanObject.Minutes * 60 * 1000;
                }
            }

        if (TimeSpanObject.Seconds > 0)
            if (FunctionGet.DisplaySeconds == true)
                CurrentSeconds += TimeSpanObject.Seconds;
            else
            {
                if (FunctionGet.DisplayMilliseconds == true)
                    CurrentMilliseconds += (long)TimeSpanObject.Seconds * 1000;
            }

        if (TimeSpanObject.Milliseconds > 0)
            if (FunctionGet.DisplayMilliseconds == true)
                CurrentMilliseconds += TimeSpanObject.Milliseconds;

        if (CurrentDays > 0)
        {
            CurrentTimeText = CurrentDays.ToString();

            if (CurrentDays == 1)
                CurrentTimeText += " Day";
            else
                CurrentTimeText += " Days";

            TimeTextList.Add(CurrentTimeText);
        }

        if (CurrentHours > 0)
        {
            CurrentTimeText = CurrentHours.ToString();

            if (CurrentHours == 1)
                CurrentTimeText += " Hour";
            else
                CurrentTimeText += " Hours";

            TimeTextList.Add(CurrentTimeText);
        }

        if (CurrentMinutes > 0)
        {
            CurrentTimeText = CurrentMinutes.ToString();

            if (CurrentMinutes == 1)
                CurrentTimeText += " Minute";
            else
                CurrentTimeText += " Minutes";

            TimeTextList.Add(CurrentTimeText);
        }

        if (CurrentSeconds > 0)
        {
            CurrentTimeText = CurrentSeconds.ToString();

            if (CurrentSeconds == 1)
                CurrentTimeText += " Second";
            else
                CurrentTimeText += " Seconds";

            TimeTextList.Add(CurrentTimeText);
        }

        if (CurrentMilliseconds > 0)
        {
            CurrentTimeText = CurrentMilliseconds.ToString();

            if (CurrentMilliseconds == 1)
                CurrentTimeText += " Millisecond";
            else
                CurrentTimeText += " Milliseconds";

            TimeTextList.Add(CurrentTimeText);
        }

        if (TimeTextList.Count > 0)
        {
            TimeTextList.Remove(TimeTextList.Last());

            if (TimeTextList.Count > 0)
                if (TimeTextList.Count > 1)
                    FunctionResult = string.Join(", ", TimeTextList) + " and " + CurrentTimeText;
                else
                    FunctionResult = TimeTextList.First() + " and " + CurrentTimeText;
            else
                FunctionResult = CurrentTimeText;
        }
        else
            FunctionResult = "0 Milliseconds";

        return FunctionResult;
    }
}

Example of usage :

string etc =
    Tools.TimeTools.MakeReadableTimeType.MakeReadableTime
    (
        new TimeTools.MakeReadableTimeType.GetMakeReadableTimeType()
        {
            DisplayDays = true
            ,
            DisplayHours = true
            ,
            DisplayMinutes = true
            ,
            DisplaySeconds = false
            ,
            DisplayMilliseconds = true
            ,
            SourceMilliseconds = 1600001351
        }
    );

result : etc = "18 Days, 12 Hours, 26 Minutes and 41351 Milliseconds"

  • 1
    Good job! but it is totally not necessary, unsafe and unproductive to implement .Net Framework common features by yourself unless it adds a value to your product. I don't see why you couldn't develop on top of `TimeSpan` for this purpose. So no votes from me! – Bizhan Nov 24 '18 at 16:37
0

You could use TimeSpan to do it:

var ts = TimeSpan.FromMilliseconds(60000);

Console.WriteLine($"Minutes: {ts.Minutes} - Seconds: {ts.Seconds} - Miliseconds: {ts.Milliseconds}");

ts = TimeSpan.FromMilliseconds(60500);

Console.WriteLine($"Minutes: {ts.Minutes} - Seconds: {ts.Seconds} - Miliseconds: {ts.Milliseconds}");

1

Tân
  • 1
  • 15
  • 56
  • 102