How can I tell how many minutes have passed between Now() and 7am that day? - ms-access

Basically I just need to know how much time has passed from a certain time that day till Now() this will be run on a timer throughout the day and used to determine when something should be run (this might seem odd but there is logic behind it).
The issue with the code below is that it gives me a very high negative number. I can only assume that this is caused from the TimeSerial not actually containing a date and only the time so it throws everything off.
Can anyone point me in the direction of a way to do what I want? I am certain that the answer is something super simple that I am missing but I haven't been able to find it.
DateDiff("n",Now(),TimeSerial(07,0,0))

You want the number of minutes from 7 AM until now. Your DateDiff had those two swapped around and that's why you got a negative value.
The reason the magnitude of that number was so large is you were asking for the difference between 07:00 on Dec 30 1899 and today. This is what that TimeSerial expression gives you ...
? Format(TimeSerial(07,0,0), "mmm d yyyy, hh:nn:ss")
Dec 30 1899, 07:00:00
I think this is what you want instead ...
DateDiff("n", Date + #07:00#, Now)

Related

MS Access, Conditional Formatting for Short Time Value highlight above 06:30

I don't see any related questions to conditional formatting regarding a time value. I have a report that needs to highlight any times greater than 06:30. I had the field set to Medium Time (06:30 AM) but the AM/PM may be an issue so now I am trying simply short time.
I have tried every combination or Left, Right, Hour, Minute - the real issue is simply the 30 minutes between 06:00 and 06:30 that is the issue. I can do a simple "Expression is Left([Time],2)>5 to get anything above 06:00, but I don't want anything from 06:00 to 06:30 highlighted.
Is the solution to this converting the time to a number first, and then applying the conditional format?
Never use strings for handling date and time. No exceptions.
You have dedicated functions for this, like:
DateDiff("n", #06:30:00#, [TimeField]) > 0
or even:
DateDiff("n", TimeSerial(6, 30, 0), [TimeField]) > 0
If a date value is included, strip that with TimeValue:
DateDiff("n", #06:30:00#, TimeValue([TimeField])) > 0

MYSQL time diff

I'm trying to calculate time diff between two time fields. Because the fields are just time with no date, I can't use timestampdiff, so I'm using timediff() or subtime(). The only problem with those is when the second time is less than the first time, it returns a negative timediff for me. I understand it's taking the times as the same day times, but is there any way to get a behavior where it always calculates the time forward? For example, I have 23:00:00 and 07:00:00.
timediff('07:00:00','23:00:00') will return a negative value, because 23:00 is greater than 07:00, but I want to get 8 hours as the return. Is there any way I can do that?
You can achieve that with an if statement:
select if(dateA > dateB,
timediff(dateA, dateB),
addtime(timediff(dateA, dateB), '24:00:00.000000'))
This way, if the first date is smaller than the second, you add 24 hours to the difference.

With the last.fm API, is there a way to get the top tracks for a time period with a supplied start and end date?

Is there anyway to use user.getTopTracks for a time period, with a start and end, as opposed to a time duration?
I know you can send a period parameter such as 7day, 3month etc, which returns the top track for that period up until the present day, but is there anyway to get the top tracks like this: for the time period 1 Jan 07 - 31 Feb 07?
Thanks for reading.
From here http://www.last.fm/api/show?service=301 it's the only way you can access the API, but unfortunately I think there's no other way. You'll have to use fixed periods. That's a limitation.

Get the next xx:30 time (the next half-past hour) after a specified time

I'm trying to find a MySQL query which will obtain the time that is the next half-past-the-hour from a specified datetime. I explicitly mean the next half-past-the-hour, not the next half-hourly point.
So for instance:
If the datetime was "2009-10-27
08:15:24", the answer would be
"2009-10-27 08:30:00"
If the
datetime was "2009-10-27 08:49:02",
the answer would be "2009-10-27
09:30:00".
I came across this page which refers to SQL Server, and towards the end of that thread there is a similar sort of problem. But it's not quite the same, and it relies on a function that MySQL doesn't have.
Here is a fuller list of examples and expected return values:
2009-10-27 08:15:24 should return 2009-10-27 08:30:00
2009-10-27 08:49:02 should return 2009-10-27 09:30:00
2009-10-27 23:49:10 should return 2009-10-28 00:30:00
2009-10-27 10:30:00(.000001) should return 2009-10-27 11:30:00
(Note how, in the fourth example, because the exact half-past (10:30:00.0000000) has already gone, the next half-past-the-hour point is found.)
I tried using this kind of thing:
SELECT IF( (MINUTE(NOW()) < 30), HOUR(NOW()), (HOUR(NOW()) + 1) )
(after which addition of a CONCATed string would take place), but it would fail because of the changeover to another day, and it feels inherently 'hacky'.
Can anyone suggest a suitable sort of algorithm? I wouldn't expect a full answer (though that would be nice!), but suggestions as to the kind of algorithm would be helpful. I've been drawing over bits of paper for two hours now! I have a hunch that using modulo might be useful but I'm not sufficiently familiar with using it.
The answer will be fed to a PHP class later, but I'd rather implement this at SQL level if possible, as the rest of query also performs other date comparison functions efficiently.
This is a little messy, but works:
select from_unixtime( floor((unix_timestamp(MY_DATE)+(30*60))/(60*60))*(60*60) + (30*60) )
It pushes the time forward 30 minutes, then truncates to the top of the hour, then adds 30 minutes to it. Because it's working unix timestamps (seconds since 1970), you don't have to worry about the boundaries of days, months, years, etc.
I can't help but notice that this would be much easier at the PHP level :-) That said, here's what you can do:
Add 30 minutes to your datetime using DATE_ADD(); this will move to the next hour if it's already past half-hour
Create a new datetime value by extracting date / hour and hard coding minutes / seconds. CONVERT(), ADDTIME() and MAKETIME() all help.
The end result is:
select ADDTIME(
CONVERT(DATE(DATE_ADD(yourDateTime, INTERVAL 30 MINUTE)), DATETIME), # date part
MAKETIME(HOUR(DATE_ADD(yourDateTime, INTERVAL 30 MINUTE)), 30, 0) # hour + :30:00
) from ...
Use the MAKETIME(hour,minute,second) function to construct the desired value.

Get the week number from a given date

Examples:
'DD/MM/YYYY
"1/1/2009" should give `1`
"31/1/2009" should give `5`
"1/2/2009" should also give `5`
Format("1/2/2009", "ww") returns 6.
So, how can I get the correct result?
It's doing two things here which don't match your expectations, I think:
Assuming you want the week with Jan 1 in as week 1, and using Sunday as first day of the week
So it has week 1 running from Sunday 28th December 2008 to Saturday 3rd Jan 2009.
Week 6 would begin on Sunday 1st Feb by this method.
The ISO standard is for week 1 to be the one containing 4 days of January, or the first Thursday of the year (different ways of expressing the same thing).
You can specify this method of calculation and the first day of the week:
Format(SomeDate,"ww",vbMonday,vbFirstFourDays)
see here for syntax:
https://support.office.com/en-US/article/Format-Function-6F29D87B-8761-408D-81D3-63B9CD842530
Regardless of the day of the week your week starts on, you need to pass unambiguous date values. "31/1/2009" can only be one date (Jan 31st), but "1/2/2009" could be Jan. 2 (US style) or Feb. 1st (everybody else who has more sense that we USAns).
In this case, I'd use DateSerial() to make sure the date is not misinterpreted:
Format(DateSerial(2009,2,1), "ww", vbMonday)
While this is not causing your problem, because Access helpfully utilizes your system's localized date settings, I think it's something you should do anyway. You certainly are forced to do so in SQL in Access, so I don't think it's a bad habit in code and expressions.
This might work: Format(YourDate, "ww",vbMonday)
"Correct result" depends on the locale. Maybe VBA will let you pick a calendar-system, otherwise you're pretty much out of luck.
Note that First-Day-On-xxDay isn't your only problem. There is also variation on what a complete week is so Week 1 in one system could be Week 53 of the previous year in another system.
So test thoroughly and don't be seduced to "correct by 1".
There is a whole standard for week numbers: ISO-8601
http://en.wikipedia.org/wiki/ISO_8601#Week_dates
I had the same problem.
It showed week 53 and week 1, yet days in week 53 and week 1 are all in week 1
I first tried changing the date format in the Access Query to this:
OrderWeek: Format([OrderDate],"yyyy-ww",1,3) <-- But it did not do the trick.
You get dates like 2014-52 for week 52 and 2015-52 where it was week 1 before.
Also the sorting was not how I liked. It sorted the data as 2014-1, 2014-11, 2014-2 etc. I want it to show as 2014-01, 2014-02 .. 2014-11 etc.
So here is the new code to display both the year and the week correctly in an Access Query:
ActualWeek: IIf(DatePart("ww",[SomeDate])=53,DatePart("yyyy",[SomeDate])+1,DatePart("yyyy",[SomeDate])) & "-" & IIf(DatePart("ww",[SomeDate])=53,"01",IIf(DatePart("ww",[SomeDate])<10,"0" & DatePart("ww",[SomeDate]),DatePart("ww",[SomeDate])))
This now shows any days from week 53 as being part of week 1
If sunday is the first day of the week (as it is in some locales) then 6 is the correct weeknumber for "1/2/2009" (february 1. 2009)
In terms of the sorting, I had the same issue and used this code to resolve it:
IIf(Format([SomeDate],"ww")<10,Format([SomeDate],"yyyy-") & "0" & Format([SomeDate],"ww"),Format([SomeDate],"yyyy-ww"))
If the week number is less than 10, add a zero, else leave it as is.
Now the sorting is fine. Hope this helps somebody.