How do I get epoch time in C#? [duplicate]
TimeSpan t = DateTime.UtcNow – new DateTime(1970, 1, 1); int secondsSinceEpoch = (int)t.TotalSeconds; Console.WriteLine(secondsSinceEpoch);
TimeSpan t = DateTime.UtcNow – new DateTime(1970, 1, 1); int secondsSinceEpoch = (int)t.TotalSeconds; Console.WriteLine(secondsSinceEpoch);
Try this: Date date = new Date(logEvent.timeSTamp); DateFormat formatter = new SimpleDateFormat(“HH:mm:ss.SSS”); formatter.setTimeZone(TimeZone.getTimeZone(“UTC”)); String dateFormatted = formatter.format(date); See SimpleDateFormat for a description of other format strings that the class accepts. See runnable example using input of 1200 ms.
You almost had it right. You just did not use a named constructor: void main() { print(DateTime.now().millisecondsSinceEpoch); } Gives: 1351441456747 See the API documentation for more: https://api.dart.dev/stable/2.10.1/dart-core/DateTime-class.html
If you want to convert a python datetime to seconds since epoch you could do it explicitly: >>> (datetime.datetime(2012,4,1,0,0) – datetime.datetime(1970,1,1)).total_seconds() 1333238400.0 In Python 3.3+ you can use timestamp() instead: >>> datetime.datetime(2012,4,1,0,0).timestamp() 1333234800.0 Why you should not use datetime.strftime(‘%s’) Python doesn’t actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it’s … Read more
The solution is : Long tsLong = System.currentTimeMillis()/1000; String ts = tsLong.toString();
datetime.datetime.fromtimestamp will do, if you know the time zone, you could produce the same output as with time.gmtime >>> datetime.datetime.fromtimestamp(1284286794) datetime.datetime(2010, 9, 12, 11, 19, 54) or >>> datetime.datetime.utcfromtimestamp(1284286794) datetime.datetime(2010, 9, 12, 10, 19, 54)
I think I have a simpler solution — set the initial date to the epoch and add UTC units. Say you have a UTC epoch var stored in seconds. How about 1234567890. To convert that to a proper date in the local time zone: var utcSeconds = 1234567890; var d = new Date(0); // The … Read more
It appears to me that the simplest way to do this is import datetime epoch = datetime.datetime.utcfromtimestamp(0) def unix_time_millis(dt): return (dt – epoch).total_seconds() * 1000.0
UPDATE 2020 You can do this with DateTimeOffset DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(epochSeconds); DateTimeOffset dateTimeOffset2 = DateTimeOffset.FromUnixTimeMilliseconds(epochMilliseconds); And if you need the DateTime object instead of DateTimeOffset, then you can call the DateTime property DateTime dateTime = dateTimeOffset.DateTime; Original answer I presume that you mean Unix time, which is defined as the number of seconds since … Read more
Early versions of unix measured system time in 1/60 s intervals. This meant that a 32-bit unsigned integer could only represent a span of time less than 829 days. For this reason, the time represented by the number 0 (called the epoch) had to be set in the very recent past. As this was in … Read more