I am trying to convert a sample java code to c#. This is the java code and I need some assistance in figuring out the proper way to get the equivalent code in c#. This is what I tried so far.
java code
TimeZone kenyaTimeZone = TimeZone.getTimeZone("Africa/Kenya");
Calendar calendar = Calendar.getInstance(kenyaTimeZone);
long timestamp = calendar.getTimeInMillis() / 1000;
C# code
TimeZoneInfo tzone = TimeZoneInfo.FindSystemTimeZoneById("Africa /Kenya");
DateTimeOffset dt = new DateTimeOffset(DateTime.UtcNow, tzone.BaseUtcOffset);
This is where I am stuck. Please help me out. I would like to get the result in milliseconds and I am unable to figure out the proper way to do it.
要将两个日期时间之间的时差转换为毫秒:
double totalMs = (endDate - startDate).TotalMilliseconds;
Maybe, this short code could help you. I read that java function "calendar.getTimeInMillis()" returns the milliseconds since 1970-01-01 (Like Unix Timestamp).
var dt = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("E. Africa Standard Time"));
// Get C# time in milliseconds (since 1900-01-01)
double milli = new TimeSpan(dt.Ticks).TotalMilliseconds;
// Get Unix timestamp
double unix = dt.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
Console.WriteLine("Total Milliseconds: {0} Unix: {1}", milli, unix);
use this :
decimal miliseconds = myDateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
myDateTime is a DateTime object
good luck :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.