Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - How to convert Timestamp to Date?

Tags:

date

c#

asp.net

I'm getting timestamp from xml document.Now, I want to convert Timestamp to Date format(13-May-13)

XmlNodeList cNodes = xncomment.SelectNodes("comment");
foreach (XmlNode node in cNodes)
{
    //I'm getting this "1372061224000" in comment-date
    string comment_date = node["creation-timestamp"].InnerText;
}

Any ideas? Thanks in advance.

like image 608
user2500094 Avatar asked Jun 26 '13 10:06

user2500094


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C programming used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

Given that this looks like a Java timestamp, simply use below:

var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Math.Round(1372061224000 / 1000d)).ToLocalTime();
Console.WriteLine(dt); // Prints: 6/24/2013 10:07:04 AM
like image 95
Teoman Soygul Avatar answered Oct 13 '22 21:10

Teoman Soygul


Finally i found how to convert time stamp to Date & Date to time stamp . I found some places in project people keep date as time stamp for get difference quickly. so in this case they use to keep the table column as Int or time stamp. now the problem is that in the application while showing the data, you need to convert it into date variable. So for that we can use the following code to convert time stamp to Date

int ts = 1451174400;
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(ts).ToLocalTime();
string formattedDate = dt.ToString("dd-MM-yyyy");

Now you can get any date format from this variable.

In the second case if you want to convert Date to time stamp then check the following code.

int ts = (dt.Ticks - 621356166000000000) / 10000000;

Where dt is the date time variable & holding a date value.

like image 21
Sapnandu Avatar answered Oct 13 '22 22:10

Sapnandu