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:
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:
Given below are the examples of Timestamp to Date C#:
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:
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:
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:
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.
The above is the detailed content of Timestamp to Date C#. For more information, please follow other related articles on the PHP Chinese website!