Updated April 1, 2023
Introduction to Timestamp to Date C#
The following article provides an outline for Timestamp to Date C#. Timestamp in C# gives the representation of time measured in a number of seconds since the first epoch of Unix is considered. This timestamp then gets converted into a date as per requirement, where the time stamp needs to be changed into date format for proper representation. Date and time representation both is necessary to get the information of any file or folder. Timestamp to date conversion is also sometimes required to get the exact and accurate detail about the package and plays an important role in representation.
Syntax of Timestamp to Date C#
For conversion of Timestamp to date in C# epochs play a vital role and that in turn have different syntax and conversion process represented as follows:
- Select a conventional date of choice.
- Then try to make and convert the System.date time equivalent to UNIX epoch.
- Add the required number of seconds for the UNIX timestamp to convert.
- Date and time present within the string use some standard representation which is used for formatting the DateTime object present within the method.
- Then the date and time need to be printed accordingly to verify whether the change has been reflected or not.
How to Convert Timestamp to Date in C#?
Timestamp and datestamp play a very important role in the day to day life; based on this, some of the important and crucial information can be retrieved at the time of packaging or even at the time of production.
Steps to convert timestamp to date in C# which are as follows:
- Timestamp in C# is always present in the Unix format, where epochs for conversion play a significant role for conversion from one format of the timestamp to date format.
- At first, the conventional date with time is taken, then the System.date time present as part of the standard library is called for performing the manipulation and conversion.
- The converted format should be in Unix epoch format for a date; otherwise, it won’t be compatible with the timestamp; as mentioned earlier, timestamp clearly supports for Unix shell.
- Some number of seconds is created for the Unix timestamp to attach and get it converted.
- Once both the string of Date and time present within the string becomes compatible following the standard and includes the formatting with the DateTime object that is present within the method is used to maintain it throughout.
- The method included the conversion of both dates to time and vice-versa conversion format. Also, it is required to make them equivalent both ways.
- Version compatibility also matters in this as it helps in making the C# to consult the respective epoch and in versions .NET 4.6 and above all of the above feature and standard library supporting this for making the plugin change into the respective.
- Some depend on the UTC timings, which have its own set of rules and restrictions like it does not get change with change in the season rather it comes with the jurisdiction of tracking the day time saver time with local time zone which either runs four hours ahead or late again depending on the situation.
Examples of Timestamp to Date C#
Given below are the examples of Timestamp to Date C#:
Example #1
This program demonstrates the Unix timestamp conversion into date timestamp, and the UNIX timestamp supports for date 10/17/2019 with a time of 3:58 PM as shown in the output.
Code:
using System;
namespace My_Workspce
{
class Progrm_1
{
static void Main (string [] args)
{
double timestmp = 1413561532;
System.DateTime dat_Time = new System.DateTime(1965, 1, 1, 0, 0, 0, 0);
dat_Time = dat_Time.AddSeconds(timestmp);
string print_the_Date = dat_Time.ToShortDateString() +" "+ dat_Time.ToShortTimeString();
System.Console.WriteLine(print_the_Date);
}
}
}
Output:
Explanation:
- The example illustration is quite simple. Moreover, it helps simplify the overall timestamp in Unix with date conversion as it includes system.datetime object, which takes care of the addition of seconds and further manipulation of the timestamp with both.
- It makes the representation and understanding easy. It can be any time format to be considered for conversion into date as shown.
Example #2
This program demonstrates the Unix timestamp to Date time conversion, where the timestamp also includes conversion and reflection of calculated millisecond also as shown in the output below.
Code:
using System;
namespace My_Workspace
{
class Program_test_0
{
static void Main (string [] args)
{
long time_srch = 124045809621000;
time_srch /=1000;
DateTime rslt = DateTimeOffset.FromUnixTimeMilliseconds(time_srch).DateTime;
Console.WriteLine(rslt);
}
}
}
Output:
Explanation:
- The Unix conversion of timestamp from time to date sometimes includes calculation of milliseconds if in case the seconds also comes up to be same or for any detailed information about time this conversion of the long type of timestamp with date helps in adding of seconds and then milliseconds also into the final result as shown in the output above.
- The epoch with string could also have been taken, but the long is taken with the timestamp in order to maintain the version compatibility.
Example #3
This program demonstrates all conversions possible in C# with respect to subtract on timestamp conversion or the date-time conversion taking into account the negative value as shown in the output below.
Code:
using System;
namespace Myworkspace_0
{
class Program_1
{
static void Main (string[] args)
{
System.DateTime dt_1 = new System.DateTime(1997, 6, 3, 22, 15, 0);
System.DateTime dt_2 = new System.DateTime(1960, 12, 6, 13, 2, 0);
System.DateTime dt_3 = new System.DateTime(1998, 10, 12, 8, 42, 0);
System.TimeSpan dfnr_1 = dt_2.Subtract(dt_1);
System.DateTime dt_4 = dt_3.Subtract(dfnr_1);
System.TimeSpan dfrn_2 = dt_2 - dt_3;
System.DateTime dt_5 = dt_1 - dfrn_2;
Console.WriteLine(dt_5);
Console.WriteLine(dfrn_2);
Console.WriteLine(dt_4);
Console.WriteLine(dfrn_2);
}
}
}
Output:
Explanation:
- This subtracts function in C# is used to synchronise the date timestamp and get the value set in a customized way, which can be changed accordingly if the value lies in a specified range as shown in the output.
Conclusion
Timestamp to date in C# or any other programming language behaves in a different way again depending upon the type of requirement. It is very important to deal with the time stamp as every application somehow includes these timestamps to maintain the consistency and detail in one place for later reference.
Recommended Articles
This is a guide to Timestamp to Date C#. Here we discuss the introduction, how to convert timestamp to date in C#? and examples respectively. You may also have a look at the following articles to learn more –