Date difference in Dart for Flutter - date

Playing in Flutter, using Dart - I am trying to establish the amount of seconds between two dates.
But for some reason the date1.difference(date2).inSeconds gives a result that does not make sense to me. Maybe it is late and I am too tired to miss something here:
Here is my code: (i.e. print statements of the dates and its supposed difference in seconds):
print(myDate); // DateTime type
print(queryDate); // DateTime type
print(myDate.difference(queryDate).inSeconds);
And the print-results say:
2019-02-01 00:18:00.000Z // myDate
2019-02-01 01:17:18.859431 // queryDate
41 // supposedly difference in seconds...
But shouldn't it be much more than 41 seconds ????
Could the reason be the x.000Z vs. .859431 format differences ? And if yes, why ?
Why is the difference method ignoring minutes and hours ?

Try this package, Jiffy. Handle all of your questions. It follows the momentjs library and respect the number of days in a month and leap years to try to get the at least best result

I found a solution:
Turns out, the queryDate was in another format (not sure what .859431 means - maybe somebody can explain ...?)
At least, when I do the following, it works:
DateTime queryDate2 = DateTime.utc(
queryDate.year,
queryDate.month,
queryDate.day,
queryDate.hour,
queryDate.minute,
queryDate.second);
Then my print-statements are:
print(myDate); // DateTime type in UTC
print(queryDate); // DateTime type in .859431 format (??)
print(queryDate2); // DateTime type in UTC
print(myDate.difference(queryDate2).inHours);
print(myDate.difference(queryDate2).inMinutes);
print(myDate.difference(queryDate2).inSeconds);
And the print-results say:
2019-02-01 00:18:00.000Z // myDate
2019-02-01 02:01:21.081575 // queryDate
2019-02-01 02:01:21.000Z // queryDate2
-1 // difference in hours (now correct !)
-103 // difference in minutes (now correct !)
-6201 // difference in seconds (now correct !)
Yes, more like it :) (..now hours, minutes and seconds are correct).
Maybe there is another way of changing a date to UTC ? Any idea appreciated.

Related

millisecondSinceEpoch returning weird value when parsing date around 1969

I don't know if this is a bug or if I'm using this method incorrectly.
Can someone explain to me why I got a weird value when parsing this date?
This is the minimal script:
void main(){
DateTime zero = DateTime.fromMillisecondsSinceEpoch(0);
print(zero.millisecondsSinceEpoch);
DateTime errorDate2 = DateTime(1969,1,1);
print(errorDate2.millisecondsSinceEpoch);
DateTime errorDate = DateTime(1969,10,2);
print(errorDate.millisecondsSinceEpoch);
}
Based on my sample, the result seems fine when I parse dates bigger than 0 milliseconds since the epoch.
But it gets weird when I parse some date less than 0 milliseconds since the epoch.
From that script, I got this output
0
-31536000000
-7862400000
I parse that output to this website.
In the first and second results, the results are fine, but in the third result, I got a date around 1720.
Can someone explain to me what happens to that function? Did I use it wrong?
What should I do when I want to parse a date less than 0 milliseconds since the epoch?
In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system. Most versions of Unix, for example, use January 1, 1970 as the epoch date; Windows uses January 1, 1601; Macintosh systems use January 1, 1904, and Digital Equipment Corporation's Virtual Memory System (VMS) uses November 17, 1858.
So here epoch time starts on January 1, 1970, so it will give you a minus value for milliseconds.
You can still parse the date. it will gave just minus value nothing else.
void main() {
DateTime zero = DateTime.fromMillisecondsSinceEpoch(0);
print(zero.millisecondsSinceEpoch);
DateTime errorDate2 = DateTime(1969,1,1);
print(errorDate2.millisecondsSinceEpoch);
DateTime errorDate = DateTime(1969,10,2);
print(errorDate.millisecondsSinceEpoch);
DateTime error3 = DateTime.fromMillisecondsSinceEpoch(errorDate.millisecondsSinceEpoch);
print(error3);
}
Result is
0
-31555800000
-7882200000
1969-10-02 00:00:00.000
It seems that the bug comes from the website that I refer to it.
Thanks! #jamesdlin

Flutter: DateFormat.parse returning wrong time at midnight only on Desktop Win32

I'm trying to parse a string into a DateTime in flutter.
On android, iOS and Web the procedure works perfectly, but on windows it keeps returning, only at midnight, the wrong time.
DateFormat('HH:mm').parse('00:25');
returns 1970-01-01 01:25:00.000 on windows, but 1970-01-01 00:25:00.000 on any other OS, while any hour >= 1 returns the right value.
If I try using parseStrict method, the following happens
DateFormat('HH:mm').parseStrict('00:25');
returns FormatException: Error parsing 00:25, invalid hour value: 0 in it with time zone offset 1:00:00.000000. Expected value between 1 and 1. Date parsed as 1970-01-01 01:25:00.000..
Thanks in advance for any help
Edit: This DateTime is in local timezone, I'm not trying to parse a UTC time. I've noticed, though, that if I add the date, the DateTime seems to get parsed properly.
But I need only the time.
These values are the open hours for a shop.
I know that I SHOULD use TimeOfDay class, but I can't use it because it doesn't have the compareTo method, which I need for my case scenario.
Anyway, I've tried Android, iOS and Web and they all parse the time properly, the only one problematic is Windows

Calculate the time difference

I am trying to calculate time difference between 2 ZonedTime dates in Scala. I am receiving dates in "2021-03-19T15:39:42.834248-07:00" format as a String. I need the difference in seconds between 2 dates in Scala. How to convert the string to zoned time and calculate the difference?
You'll want to use the between() method as offered on a temporal.ChronoUnit.
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit.SECONDS
val start = ZonedDateTime.parse("2021-03-19T15:39:42.834248-07:00")
val stop = ZonedDateTime.parse("2021-03-19T15:49:42.834248-08:00")
val secsBetween:Long = SECONDS.between(start, stop) // 4200
An alternative is to use the until() method on the ZonedDateTime instance itself.
val secsBetween:Long = start.until(stop, SECONDS) //same result
[Java syntax, not Scala.]
tl;dr
Duration
.between
(
OffsetDateTime.parse( "2021-03-19T15:39:42.834248-07:00" ) ,
OffsetDateTime.parse( "2021-03-19T15:49:42.834248-08:00" )
)
.toString()
See this code run live at IdeOne.com.
PT1H10M
…which in standard ISO 8601 format means 1 hour and 10 minutes.
Details
The Answer by jwvh is close, but I would change a couple things.
OffsetDateTime, not ZonedDateTime
Your input strings have only a mere offset-from-UTC but no time zone. So parse those as OffsetDateTime.
An offset is simply a number of hours-minutes-seconds ahead or behind the baseline of UTC, the line drawn through Royal Observatory, Greenwich. An example of an offset is -07:00 which means seven hours behind UTC.
A time zone is much more. A time zone is history of the past, present, and future changes to the offset used by the people of a particular region. A time zone has a name in format of Continent/Region. Given our example above, on some dates, several time zones may share the offset of -07:00, including America/Dawson, America/Los_Angeles, America/Phoenix, America/Boise, and more.
OffsetDateTime odt = OffsetDateTime.parse( "2021-03-19T15:39:42.834248-07:00" ) ;
Duration
Represent a span-of-time using Duration, on the scale of hours-minutes-seconds-nanos.
Duration d = Duration.between( sooner , later ) ;
Generate text in standard ISO 8601 format.
String output = d.toString() ;

Calculating Seconds Until a Given Date and Time in Joda Time

I am trying to use the Joda Time library to help me schedule sending some messages to an Actor in Akka.
I would like to schedule sending emails every day at 8:30 AM. To do this, I have to tell the scheduler how many seconds (or milliseconds) to wait until the next message is sent.
I would like to account for daylight savings (to make sure it always fires around 8:30, and not 7:30 or 9:30) so I will use LocalDate and LocalTime.
So, basically, I have:
targetDate = LocalDate.now().plusDays(1) and targetTime = new LocalTime(8, 30)
and
rightNow = LocalDateTime.now()
I was wondering what is the best way to compose a targetDateTime based on targetDate and targetTime so I can use it to compute the time difference with rightNow
I know I can create a new LocalDateTime extracting all the values for the constructor from my targetDate and targetTime but: is there a more elegant way?
So far, I have settled for:
targetDateTime = targetDate.toLocalDateTime(targetTime)
secondsToWait = Seconds.secondsBetween(rightNow, targetDateTime)
Getting targetDateTime is easy if you have the targetDate and targetTime (as given in your question) :
targetDateTime = targetDate.toDateTime(targetTime);
Getting the seconds of the Duration between now and targetDateTime:
new Duration(new DateTime(), targetDateTime).getStandardSeconds();
The method is called standard seconds because it assumes every second to be a standard second of 1000 milliseconds. As its javadoc says, currently all Chronologies only have standard seconds.
But you can also simply use milliseconds (no conversion assumptions needed) :
new Duration(new DateTime(), targetDateTime).getMillis();
Disclaimer : I only just saw this was a scala question, so you may have to correct for any syntax differences, since I'm not versed in scala.

ASP: convert milliseconds to date

I need to convert a field with milliseconds created by PHP app to a date value. Is there a way to do this using VBScript? or convert to datetime in SQL 2005?
Example: 1113192000 to mm/dd/yyyy hh:mm:ss
Something like the following should work:
Function ConvertPhpToDate(numSeconds)
Dim dEpoch
dEpoch = DateSerial(1970,1,1)
ConvertPhpToDate = DateAdd("s",numSeconds,dEpoch)
End Function
Note, the php time() function returns the number of 'seconds', not milliseconds. http://php.net/manual/en/function.time.php
I think what you are referring to as milliseconds is really the epoch time as returned by the php's time/date functions. You can give this a function a shot to get the epoch time converted to datetime format in ASP:
function epoch2date(myEpoch)
epoch2date = DateAdd("s", myEpoch, "01/01/1970 00:00:00")
end function
Source: http://www.epochconverter.com/epoch/functions.php#asp
msValue = 32312312
dtValue = DateAdd("s", msValue/1000, CDate("1970-01-01 00:00:00"))
Wrap it in a function:
Function TimestampToDate(timestamp)
TimestampToDate = DateAdd("s", timestamp/1000, CDate("1970-01-01 00:00:00"))
End Function
You'll need to have a base time to count the milliseconds from e.g. 1st Jan 1970 or similar.
You then divide the number of milliseconds by 1000 to get the number of seconds - saving the remainder.
Divide the number of seconds by 60 (saving the remainder) to get the number of minutes.
Then 60 again for hours, 24 for days.
Then it get's difficult as you've got leap years to consider. There is another question on this here.
Once you've got your years, days, hours, minutes, seconds and milliseconds you add this to the base date-time to get the date-time represented.
Others have posted code etc. that you might use.