I'm trying to set the default value for a date parameter to the first business day of next month, that is if the 1st of the month is a Saturday or a Sunday, it selects the following Monday.
I've got this but for some reason when I try to run the report it says an error occurred during local report processing and I can't figure out what's going wrong. Can anyone help?
= iif (datepart("dw", dateadd("m",1,DateAdd("d",1-DatePart("d",Today()),Today()))) = 7,
dateadd("m",1,DateAdd("d",3-DatePart("d",Today()),Today())),
iif (datepart("dw", dateadd("m",1,DateAdd("d",1-DatePart("d",Today()),Today()))) = 1,
dateadd("m",1,DateAdd("d",2-DatePart("d",Today()),Today())),
dateadd("m",1,DateAdd("d",1-DatePart("d",Today()),Today()))))
=Today.AddMonths(1).AddDays(-Today.Day + 1).AddDays(
SWITCH(
Today.AddMonths(1).AddDays(-Today.Day + 1).DayOfWeek = DayOfWeek.Sunday, 1,
Today.AddMonths(1).AddDays(-Today.Day + 1).DayOfWeek = DayOfWeek.Saturday, 2,
True, 0
))
This returns the 1st of the following month:
Today.AddMonths(1).AddDays(-Today.Day + 1)
The switch statement then determines how many days to add based on the day of the 1st of the following month:
.AddDays(SWITCH(
Today.AddMonths(1).AddDays(-Today.Day + 1).DayOfWeek = DayOfWeek.Sunday, 1,
Today.AddMonths(1).AddDays(-Today.Day + 1).DayOfWeek = DayOfWeek.Saturday, 2,
True, 0
))
Try:
=Switch(
WeekDay(DateSerial(Today.Year,Today.Month,1).AddMonths(1),FirstDayOfWeek.Monday)=6,
DateSerial(Today.Year,Today.Month,1).AddMonths(1).AddDays(2),
WeekDay(DateSerial(Today.Year,Today.Month,1).AddMonths(1),FirstDayOfWeek.Monday)=7,
DateSerial(Today.Year,Today.Month,1).AddMonths(1).AddDays(1),
true, DateSerial(Today.Year,Today.Month,1).AddMonths(1)
)
Assuming your first date of week monday.
Hope it helps.
Related
I am trying to retrieve dates from a date column in mySQL database. My code (mysqljs) :
today = document.getElementBYId("myDate").value
The result :
today = Thu Dec 23 2021 14:05:00 GMT+0800 (Singapore Standard Time)
How can I get it to return just the date and time (yyy:mm:dd hh:mm:ss)?
(In mySql - the column type of the field date is set to DATETIME.)
Finally I have fund a way to solve this problem.
First I capture the date into a variable :
`var today = document.getElementById("mydate").value;`
(Today is in the format :
Thu Dec 23 2021 14:05:00 GMT+0800 (Singapore Standard Time)
Then I split it (to remove those info that I do not want):
var t = today.split(" ");
var d = t[0]+" "+t[1]+" "+t[2]+","+t[3]
Next I create a function to format the date :
function formateDate(date){
var e = new Date(date),
month = '' + (e.getMonth() + 1),
day = '' + e.getDate(),
year = e.getFullYear();
if (month.length <2)
month = '0' + month;
if (day.length <2)
day = '0' + day;
return [year, month, day].join('-');
}
Then I pass today into the function :
`today_x = formatDate(d)`
Finally I display it to the form That I am rendering :
document.getElementById("mydate").value = today_x;
I hope this is helpful. I don't think this is the most efficient way but I have been struggling with this for almost a week and this is the only solution I can come up with. I hope someone can offer another solution that is less tedious.
In SSRS Expression, how to count the numbers of rows that present in today or yesterday in dataset with other equivalent condition,
For example
=COUNT(IIF(
DateDiff(DateInterval.Day,Cdate("01/01/1900"),Fields!Opendate.Value) =
DateDiff(DateInterval.Day,Cdate("01/01/1900"), Now()), Fields!Opendate.Value, Nothing))
Using this expression I can check get the total count for today, it's working.
I need to add to check today date with other condition like:
If (today date and Fields!reason.Value = "Other")
It's not working when I add reason value to check :
=COUNT(IIF(
DateDiff(DateInterval.Day, Cdate("01/01/1900"), Fields!Opendate.Value) =
DateDiff(DateInterval.Day, Cdate("01/01/1900"), Now()) -1, Fields!Opendate.Value, Nothing ) **And Fields!reason.Value = "Other"**)
Please guide me
Just add it in your IIF() statement:
=COUNT(IIF(
DateDiff(DateInterval.Day, Cdate("01/01/1900"), Fields!Opendate.Value) =
DateDiff(DateInterval.Day, Cdate("01/01/1900")
And Fields!reason.Value = "Other",
Now()) -1,
Fields!Opendate.Value,
Nothing
)
)
Good morning All,
I am working on the following however, I can't figure it out for the life of me. I have table that contains a bunch of due dates. What I am trying to do is add a query that adds another field and inserts a yes if the date equals a day in last week. Honestly I need it to insert yes as long as it does not = this week.
I've tried using:
Urgent: IIf([Due Date]=[Due Date]
Between
DateAdd("d",1-Weekday(Date()) 7, Date()) And
DateAdd("d",1-Weekday(Date())-1, Date()),"Yes","")
with no luck. What am I missing here?!
Thanks all. Cheers.
Here's how to return Urgent if 'not this week' means previous Saturday and before. Not sure how you would do it without code.
Helper Function to get the first day of this week based on current date. Paste it into a new or existing global module.
Function FirstDateOfTheWeek() As Date
Dim dt As Date
If Weekday(Date) = vbSaturday Then
FirstDateOfTheWeek = DateAdd("y", -6, Date) 'today is Saturday? then return previous Sunday
ElseIf Weekday(Date) = vbSunday Then
FirstDateOfTheWeek = Date 'today is Sunday? then return Sunday because its the first day of the week
Else 'weekday, so just go backwards until we hit previous sunday's date
dt = Date
While Weekday(dt) <> vbSunday
dt = DateAdd("y", -1, dt)
Wend
FirstDateOfTheWeek = dt
End If
End Function
An IsUrgent function to be called from query. Paste it into a new or existing global module.
Function IsUrgent(dt As Variant) As String
If IsNull(dt) Then 'if null date is passed then return blank string; Variant chosen instead of date for this case; change it to N/A if you want?
IsUrgent = ""
Exit Function
End If
If dt < FirstDateOfTheWeek Then
IsUrgent = "Yes"
Else
IsUrgent = ""
End If
End Function
The Query Column calling IsUrgent() function:
IsUrgent: IsUrgent([Due Date])
Im trying to create some VB code that will get the start and end of the previous month.
Im able to the current month which is just:
Month(DateValue(Now))
which would return 3. From there I can take away 1 to give me 2 meaning February. This is fine but what about when I Im in January and I repeat this and it gives me zero - my code will fail. Any one know how to get the previous months start and end day then?
Thanks
The first day of the previous month is always 1, to get the last day of the previous month, use 0 with DateSerial:
''Today is 20/03/2013 in dd/mm/yyyy
DateSerial(Year(Date),Month(Date),0) = 28/02/2013
DateSerial(Year(Date),1,0) = 31/12/2012
You can get the first day from the above like so:
LastDay = DateSerial(Year(Date),Month(Date),0)
FirstDay = LastDay-Day(LastDay)+1
See also: How to caculate last business day of month in VBScript
I have similar formula for the First and Last Day
The First Day of the month
FirstDay = DateSerial(Year(Date),Month(Date),1)
The zero Day of the next month is the Last Day of the month
LastDay = DateSerial(Year(Date),Month(Date)+ 1,0)
firstDay = DateSerial(Year(DateAdd("m", -1, Now)), Month(DateAdd("m", -1, Now)), 1)
lastDay = DateAdd("d", -1, DateSerial(Year(Now), Month(Now), 1))
This is another way to do it, but I think Remou's version looks cleaner.
Try this
First_Day_Of_Previous_Month = New Date(Today.Year, Today.Month, 1).AddMonths(-1)
Last_Day_Of_Previous_Month = New Date(Today.Year, Today.Month, 1).AddDays(-1)
This works reliably for me in my main sub.
Dim defDate1 As Date, defDate2 As Date
'** Set default date range to previous month
defDate1 = CDate(Month(Now) & "/1/" & Year(Now))
defDate1 = DateAdd("m", -1, defDate1)
defDate2 = DateAdd("d", -1, DateAdd("m", 1, defDate1))
Try this to get the month in number form:
Month(DateAdd("m", -3, Now))
It will give you 12 for December.
So in your case you would use Month(DateAdd("m", -1, Now)) to just subract one month.
Just to add something to what #Fionnuala Said, The below functions can be used. These even work for leap years.
'If you pass #2016/20/01# you get #2016/31/01#
Public Function GetLastDate(tempDate As Date) As Date
GetLastDate = DateSerial(Year(tempDate), Month(tempDate) + 1, 0)
End Function
'If you pass #2016/20/01# you get 31
Public Function GetLastDay(tempDate As Date) As Integer
GetLastDay = Day(DateSerial(Year(tempDate), Month(tempDate) + 1, 0))
End Function
Public Shared Function GetFOMPrev(ByVal tdate As Date) As Date
Return tdate.AddDays(-(tdate.Day - 1))
End Function
Public Shared Function GetEOMPrev(ByVal tdate As Date) As Date
Return tdate.AddDays(-tdate.Day)
End Function
Usage:
'Get End of Month of Previous Month - Pass today's date
EOM = GetEOMPrev(Date.Today)
'Get First of Month of Previous Month - Pass date just calculated
FOM = GetFOMPrev(EOM)
Can someone please tell me what is wrong with my query.
I am trying to fetch data fro my table based on the column called weekend, if weekend is set to "0" show from Sunday 6pm until Friday 9pm, then if weekend is set to "1" Show from Friday 9pm until Sunday 6pm.
SELECT *
FROM closures
WHERE closures.weekend = 0
OR WEEKDAY(NOW()) < 4
AND closures.weekend = 1
OR WEEKDAY(NOW()) > 4
OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
It helps to phrase the question properly. What you should have said is "between 9pm Friday and 6pm Sunday I want to show the rows where closures.weekend = 1, otherwise show those where closures.weekend = 0".
Hence what you need to do is generate a value of 1 or 0 depending on whether it's the weekend or not, and then SELECT those rows where weekend has that value, i.e:
SELECT *
FROM
closures
WHERE
weekend = IF(
(WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
OR (WEEKDAY(NOW()) = 5)
OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
, 1, 0)
Weekend cant be both, 0 and 1
SELECT *
FROM
closures
WHERE
closures.weekend = 0
OR
(
WEEKDAY(NOW()) < 4
AND closures.weekend = 1
)
OR WEEKDAY(NOW()) > 4
OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
My first guess is that the
SELECT field1, field2, ...
is missing ?