I would like to ask what is advantage of using the milliSecondsSincEepoch over normal DateTime in flutter?
Could anyone explain? Thank you in advnce for the answer!
return {
'createdAt': createdAt.milisecondsSinceEpoch,
'updatedAt': updatedAt.milisecondsSinceEpoch,
...
The millisecondsSinceEpoch property is the number of milliseconds since the "Unix Epoch" 1970-01-01T00:00:00Z (UTC).
This value is time zone independent.
This value is up to 8,640,000,000,000,000 milliseconds (100,000,000 days) since the Unix epoch. In other words: millisecondsSinceEpoch.abs() <= 8640000000000000.
Source: https://api.flutter.dev/flutter/dart-core/DateTime/millisecondsSinceEpoch.html
Related
What's the current time Date object time precision when it is initiated using Date()? Does it capture the time to milliseconds?
let currentTime = Date()
print(currentTime) // 2022-10-09 09:13:39 +0000
When I print the date it only shows 2022-10-09 09:13:39 +0000 so I wonder if its precision is only to the second.
Does it capture the time to milliseconds?
Yes, it does. printing a date shows a fixed string description omitting the fractional seconds. A hint is that TimeInterval is defined as a floating point type (Double).
You can prove it
let interval = Date().timeIntervalSince1970
print(interval)
which shows a real fractional part rather than a Is-floating-point-math-broken value like .00000003
Say the EPOCH timestamp I received from an API is 1595216214.
It is equivalent to Monday, July 20, 2020 3:36:54 AM (GMT).
My interest is time value only (Ignoring the date/day value)? How can I code in Dart?
Also, how can I convert it into my time zone (E.g.: GMT+8)
you can use DateTime class to do that. Like this:
var dateUtc = DateTime.fromMillisecondsSinceEpoch(myAPIEpochTimeInMilliseconds, isUtc: true);
var dateInMyTimezone = dateUtc.add(Duration(hours: 8));
var secondsOfDay = dateInMyTimezone.hour * 3600 + dateInMyTimezone.minute * 60 + dateInMyTimezone.second;
NOTE:
If you are doing this for the web, although Dart does support 64+ bit numbers, javascript only takes 32-bit integers. So, use the BigInt class for big numbers tha exceeds 32-bit representation.
DateTime doesn't have an inherent timezone to be defined on the class. Is either the local (machine) time or utc Time. So, it is recomended to always use utc and add timezone offset when needed. Or just create a wrapper.
Problem Description
I have 020-03-20T14:16:27.189282+08:00 return from my database and I am trying to convert it to 2:16pm using DateTime.parse(). However,every time I convert the time, I get 6:16am.
Question
May I know how could I solve this problem?
MyCode
dateTime = `020-03-20T14:16:27.189282+08:00`
time = DateFormat.jm().format(DateTime.parse(dateTime));
print(time);
Dai, thanks for your small tips.
Solutions for this question is remove the offset from the string.
Why
The result is always in either local time or UTC. If a time zone offset other than UTC is specified, the time is converted to the equivalent UTC time.
Read it here https://api.dart.dev/stable/2.7.1/dart-core/DateTime/parse.html.
Answered
String dateTime = `020-03-20T14:16:27.189282+08:00`;
int indexOfPlusMinusSymbol = dateTime .indexOf('+') >= 0
?dateTime .lastIndexOf('+')
: dateTime .lastIndexOf('-');
String time = DateFormat.jm().format(DateTime.parse(dateTime..substring(0, indexOfPlusMinusSymbol)));
print(time);
Is there a method in matlab to convert seconds from a known date to a standard date time format?
For example, if I have a vector of values shown as seconds from 1901/01/01, how would I convert them to a dateTime? In this case a value of 28125 would correspond to 1981/01/01. Is there an efficient method for doing this?
The numbers in your example do not make sense so it is not clear if your time is in seconds or days but since you asked for seconds I will use this.
What you want to achieve can be done using datenum function. This function returns the number of (fractional) days from 1/1/0000. So first you need to find your offset, e.g.:
offsetInDays = datenum(1901,1,1);
Next, you convert the date from seconds to days:
dateInDays = YourRequiredDateInSec * 3600 * 24;
Finally, you date is given by
RequiredDate = datestr(offsetInDays + dateInDays);
How can I display time as a number value for an if statement?
I'm trying to write an if statement where a set time is compared against my computed time for an event to occur. I just don't know how to write that time as a value. how can I write a time like 10am as a set time value for example.
Any help is much appreciated.
You can use the NSDate class and the timeIntervalSinceDate: method to compare the two times. You need to set up the two NSDate objects with the different times (and the same date) and then call that method to compare the two.
The easiest way to do this is probably to convert the time to a UNIX timestamp (unsigned integer representing the number of seconds which have elapsed since january 1st 1970)
convert both times to this format and compare.
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
To compare two dates use:
- (NSComparisonResult)compare:(NSDate *)anotherDate
Here is the definition of NSComparisonResult:
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;