15,995,080 members
See more: , +
Hello,

I am able to successfully convert timestamp to datetime using below code:

Timestamp to DateTime:
C#
```double dTimeSpan = Convert.ToDouble("1404757800000");
DateTime dtReturn = new DateTime(1970, 1, 1, 0, 0, 0,    DateTimeKind.Utc).AddSeconds(Math.Round(dTimeSpan / 1000d)).ToLocalTime();```

Result:
C#
`7/8/2014 12:00:00 AM`

But i am not able to convert this datetime to correct timestamp. Using below code because it give me wrong timestamp.

Datetime to Timestamp:
C#
```DateTime dtEPoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime dtTime = dtReturn.Subtract(new TimeSpan(dtEPoch.Ticks));
long lngTimeSpan = dtTime.Ticks / 10000;
string strTimeSpan = lngTimeSpan.ToString();```

Result:
C#
`1404777600000`

Posted
Updated 30-Jun-14 0:30am
v3

## Solution 1

Try this
C#
``` date=new Datetime(2014,8,7);
long Timestamp = date.Ticks - new DateTime(1970, 1, 1).Ticks;
Timestamp /= TimeSpan.TicksPerSecond;
return Timestamp;```

v2

## Solution 2

I think this should do the trick.

C#
```DateTime dtEPoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan timeSpan = dtReturn - dtEPoch;
string strTimeSpan = timeSpan.ToString();```

## Solution 3

Change DateTimeKind.Utc to DateTimeKind.Local then it will work
```double dTimeSpan = Convert.ToDouble("1404757800000");
DateTime dtReturn = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddSeconds(Math.Round(dTimeSpan / 1000d)).ToLocalTime();

DateTime dtEPoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
DateTime dtTime = dtReturn.Subtract(new TimeSpan(dtEPoch.Ticks));
long lngTimeSpan = dtTime.Ticks / 10000;
string strTimeSpan = lngTimeSpan.ToString();
```

Get more details Here[^]

v2
Harish Kumar Bansal 30-Jun-14 7:10am
Thanks, it is working fine by changing Utc to Local.

## Solution 4

C#
```DateTime oDT = new DateTime(2014, 2, 21);
TimeSpan oTS = new TimeSpan(oDT.Ticks);```