Datetime C# from a Timestamp

Introduction 

 
This article is about creating Datetime C# from a timestamp.
 
The sun rises at an interval. This can be predicted. Many years ago, astronomers found that planetary orbits determine the length of a day. This also informs us about Datetime C#, which is the current date or realtime DateTime in C#. Now, we are using the DateTime type. TimeSpan indicates a range of time. A timer records periods of time. A format string can be used to create a DateTime.
 
The timer measures time elapsed. It is useful for micro-benchmarks in code optimization and can perform routine and continuous performance monitoring.
 

TimeSpanToString

 
Year, month, day, hours, minutes, & seconds. The timer can handle short timings of only a few milliseconds. For longer timings, we want to display hours, minutes and seconds. Nanoseconds aren’t important here.
 
The timer is a class in the .NET Framework that is ideal for timing any operation in your C# programs. You must create a Timer as an instance.
 
Start
 
The Start method tells the Timer to store the current time. It queries Windows to find the current tick count of the system.
 
Stop
 
We call Stop on the instance Timer. This tells the Timer to capture the current tick count of the system, and is also very accurate.
 
Let’s follow the below steps to create a DateTime C#.
 
Step 1
 
Create a new application project. In Visual Studio, on the menu click File> New > Project. For more details, see the following menu on the display.

 
Step 2
 
Then, New Project will appear.

 
Step 3
 
Write down the name of the project that will be created on a field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.

 
Step 4
 
Create a new Windows form, like shown below:

 
Step 5
 
Next, go back to the Windows form and view the code in order to write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace Date_Time  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19. private void timer1_Tick(object sender, EventArgs e)  
  20.         {  
  21.             timer1.Enabled = false;  
  22.             string day = DateTime.Today.ToString("dddd");  
  23.             string year = DateTime.Now.Year.ToString("0000");  
  24.             string month = DateTime.Now.Month.ToString("00");  
  25.             string date = DateTime.Now.Day.ToString("00");  
  26.             string hour = DateTime.Now.Hour.ToString("00");  
  27.             string minute = DateTime.Now.Minute.ToString("00");  
  28.             string second = DateTime.Now.Second.ToString("00");  
  29.             string month2;  
  30.             string day2;  
  31.   
  32. switch (month)  
  33.             {  
  34.                 case "01":  
  35.                     month2 = "January";  
  36.                     break;  
  37.                 case "02":  
  38.                     month2 = "February";  
  39.                     break;  
  40.                 case "03":  
  41.                     month2 = "March";  
  42.                     break;  
  43.                 case "04":  
  44.                     month2 = "April";  
  45.                     break;  
  46.                 case "05":  
  47.                     month2 = "May";  
  48.                     break;  
  49.                 case "06":  
  50.                     month2 = "June";  
  51.                     break;  
  52.                 case "07":  
  53.                     month2 = "July";  
  54.                     break;  
  55.                 case "08":  
  56.                     month2 = "August";  
  57.                     break;  
  58.                 case "09":  
  59.                     month2 = "September";  
  60.                     break;  
  61.                 case "10":  
  62.                     month2 = "October";  
  63.                     break;  
  64.                 case "11":  
  65.                     month2 = "November";  
  66.                     break;  
  67.                 case "12":  
  68.                     month2 = "December";  
  69.                     break;  
  70.                 default:  
  71.                     month2 = "UnKnown";  
  72.                     break;  
  73.             }  
  74.   
  75. switch (day)  
  76.             {  
  77.                 case "Sun":  
  78.                     day2 = "Sunday";  
  79.                     break;  
  80.                 case "Mon":  
  81.                     day2 = "Monday";  
  82.                     break;  
  83.                 case "Tue":  
  84.                     day2 = "Tuesday";  
  85.                     break;  
  86.                 case "Wed":  
  87.                     day2 = "Wednesday";  
  88.                     break;  
  89.                 case "Thu":  
  90.                     day2 = "Thursday";  
  91.                     break;  
  92.                 case "Fri":  
  93.                     day2 = "Friday";  
  94.                     break;  
  95.                 case "Sat":  
  96.                     day2 = "Saturday";  
  97.                     break;  
  98.                 case "Sunday":  
  99.                     day2 = "Sunday";  
  100.                     break;  
  101.                 case "Monday":  
  102.                     day2 = "Monday";  
  103.                     break;  
  104.                 case "Tuesday":  
  105.                     day2 = "Tuesday";  
  106.                     break;  
  107.                 case "Wednesday":  
  108.                     day2 = "Wednesday";  
  109.                     break;  
  110.                 case "Thursday":  
  111.                     day2 = "Thursday";  
  112.                     break;  
  113.                 case "Friday":  
  114.                     day2 = "Friday";  
  115.                     break;  
  116.                 case "Saturday":  
  117.                     day2 = "Saturday";  
  118.                     break;  
  119.                 //I'm Use Jakarta Timezone, change this case from your Timezone  
  120.                 case "Minggu":  
  121.                     day2 = "Sunday";  
  122.                     break;  
  123.                 case "Senin":  
  124.                     day2 = "Monday";  
  125.                     break;  
  126.                 case "Selasa":  
  127.                     day2 = "Tuesday";  
  128.                     break;  
  129.                 case "Rabu":  
  130.                     day2 = "Wednesday";  
  131.                     break;  
  132.                 case "Kamis":  
  133.                     day2 = "Thursday";  
  134.                     break;  
  135.                 case "Jum'at":  
  136.                     day2 = "Jum'at";  
  137.                     break;  
  138.                 case "Jumat":  
  139.                     day2 = "Friday";  
  140.                     break;  
  141.                 case "Sabtu":  
  142.                     day2 = "Sunday";  
  143.                     break;  
  144.                 default:  
  145.                     day2 = "UnKnown";  
  146.                     break;  
  147.             }  
  148.             label1.Text = day2 + " , " + date + " " + month2 + " " + year + "  " + hour + ":" + minute + ":" + second;  
  149.             label1.Update();  
  150.              
  151.             timer1.Enabled = true;  
  152.         }  
  153.     }  

Step 6
 
After you write down the program listings, press the F5 key to run the program. If run successfully, the result is:

 
We have explained how to make a program show datetime, with an example in realtime. For those of you who want to download the source code of the program, you also can.
 
Hopefully this discussion is helpful to you. If you have any questions, you can contact me.
 
You can see Datetime C# from Timestamp from the Github project here.
 
Thank you for reading this article about DateTime in C# from a Timestamp, I hope this article was useful for you. Visit My Github about .NET C# here.


Recommended Free Ebook
Similar Articles