none
Converting DateTime To Currency RRS feed

  • Question

  • Hello, Developer friends,

    I hope that you are well.

    I have made some progress with my code and successfully converted the DateTime variable to a string that represents currency. My end goal is to create a clock that counts annual salary on a second-to-second basis.

    I’m having some trouble adjusting the parameters. At the moment, my clock begins to count at 00:00:01 however I wish to adjust this to make sure the clock only displays a currency value between the hours of 9:00:00 and 17:00:00 for pragmatic purposes.

    Are you able to offer any assistance? I have attached the Form.cs file to this email.

    Esentially, I believe I need to change line 54 to represent a subtraction of DateTime HH and HH1 values but am unable  to do so because it’s a string. Any help would be great.

    Thank you.

    Regards,

    <sub>Code is as follows:</sub>

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace DigitalClock
    {
        public partial class Form1 : Form
        {

            Timer t = new Timer();
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //timer interval
                t.Interval = 1000; //in milliseconds

                t.Tick += new EventHandler(this.t_Tick);

                //start timer when form loads
                t.Start(); //this will use t_Tick() method
            }

            //time evernthandler
            private void t_Tick(object sender, EventArgs e)
            {
                //get current time
                double HH = Math.Round(8.105 * (DateTime.Now.Hour), 0);
                //double HH1 = DateTime.Now.Hour;
                double mm = Math.Round(0.135 * (DateTime.Now.Minute), 0);
               // double mm1 = DateTime.Now.Minute;
                double ss = Math.Round(0.002 * (DateTime.Now.Second), 2);
               // double ss1 = DateTime.Now.Second;

                //time
                string time = "";

                    //padding leading zero
                    if(DateTime.Now.Hour < 9)
                {
                    time = "0" + HH1;
                }
                    else if (DateTime.Now.Hour > 9 && DateTime.Now.Hour < 17)
                {
                    time += "0" + HH;
                }
                    else if (DateTime.Now.Hour > 17)
                {
                    time = "0" + HH1;
                }
                    else {
                    time += HH;
                }
                time += ":";
                if (DateTime.Now.Minute < 10)
                {
                    time = "0" + mm;
                }
                //else if (DateTime.Now.Hour > 9 && DateTime.Now.Hour < 17 && DateTime.Now.Minute < 10)
                //{
                    //time += "0" + mm;
                //}
                //else if (DateTime.Now.Hour > 17)
                //{
                    //time = "0" + mm1;
                //}
                else
                {
                    time += mm;
                }
                time += ":"; if (ss < 10)
                {
                    time += "0" + ss;
                }
                else
                {
                    time += ss;
                }
                time += ":";

                //update label
                label1.Text = "$" + time;
            }
        }
    }

    <sub></sub>

    <sub>//Samuel Josling</sub>

    Tuesday, October 8, 2019 10:58 PM

All replies

  • Greetings Jamsos.

    I can't really understand what you are trying to do (sorry), but I think the tool you are looking for might be TimeSpan. That is, you can work out the time elapsed since the start of the day. Here is a short console program to illustrate.

       class Program
       {
          static void Main(string[] args)
          {
             // Calculate the start of the working day and end of the working day.
             DateTime now = DateTime.Now;
             DateTime workStart = new DateTime(now.Year, now.Month, now.Day, 9, 0,0);
             DateTime workEnd = new DateTime(now.Year, now.Month, now.Day, 17, 0, 0);
    
             // Calculate the hours worked so far today (ignoring breaks for lunck or whatever).
             TimeSpan worked = new TimeSpan(0); // Default - working day hasn't started.
             if (now > workStart && now < workEnd)
             {
                worked = now - workStart; // Sometime during working hours.
             }
             else if (now > workEnd)
             {
                worked = workEnd - workStart; // The working day has ended, so get the maximum time worked.
             }
    
             Console.WriteLine(worked.ToString());
          }
       }

    So you can calculate the amount of time worked today and do your thing with that value.

    Note that in my sample code, I only call DateTime.Now once, not every time I do a calculation with the current time. That's because every time you call DateTime.Now a small amount of time will have elapsed since the last time, so you will keep getting slightly different values. So to be consistent, just call it once at the beginning and save that value for reuse (which will be faster, as a bonus).

    Wednesday, October 9, 2019 12:18 AM
  • Adding to Ante's reply, "worked" is a TimeSpan value that represents time since the work day started.  If you want to convert that into dollars at $8.105 per hour, you just need:

        double salary = worked.TotalHours * 8.105;
        Console.WriteLine( "$", salary );


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, October 9, 2019 11:02 PM