Get last monday date - date

I want to get the last Monday date for the given date. For example If my input is 190113 I want the output as 190107 which is last Monday.
if {$current_date == "Mon"} {
set startday [clock seconds]
set startday [clock format $startday -format %y%m%d]
puts $startday
} else {
puts "no monday today"
#I don't know how to get last monday date
}

This can be done fairly simply, by taking advantage of the fact that clock scan has quite a complex parser, and you can supply a timestamp that everything is relative to via the -base option. Also, both clock scan and clock format take -format options so that you can specify exactly what is going on in your input and output data.
proc getLastMonday {baseDate} {
set base [clock scan $baseDate -format "%y%m%d"]
set timestamp [clock scan "12:00 last monday" -base $base]
return [clock format $timestamp -format "%y%m%d"]
# This would work as a one-liner, provided you like long lines
}
Demonstrating:
puts [getLastMonday 190113]; # ==> 190107
puts [getLastMonday 190131]; # ==> 190128

Reference: https://www.tcl.tk/man/tcl/TclCmd/clock.htm#M22
Here's a sample code-snippet for the purpose. Added inline comments for understanding:
proc get_last_monday_date {date} {
# Get the end timestamp for the specified date
set end_timestamp [clock scan ${date}-23:59:59 -format %y%m%d-%H:%M:%S]
# Get day of the week for the current date
set day_of_week [clock format $end_timestamp -format %u]
# Sunday may report as 0 or 7. If 0, change to 7
# if {$day_of_week == 0} {
# set day_of_week 7
# }
# Monday is 1st day of the week. Monday = 1.
# Find how many days to go back in time
set delta_days [expr $day_of_week - 1]
# Multiply the delta by 24 hours and subtract from end of the day timestamp
# Get the timestamp for the result. That's last Monday's timestamp.
return [clock format [clock add $end_timestamp -[expr $delta_days * 24] hours] -format %D]
}
puts "Last Monday for 01-Jan-2019: [get_last_monday_date 190101]"
puts "Last Monday for 06-Jan-2019: [get_last_monday_date 190106]"
puts "Last Monday for 15-Jan-2019: [get_last_monday_date 190115]"
puts "Last Monday for 31-Jan-2019: [get_last_monday_date 190131]"
Execution output:
Last Monday for 01-Jan-2019: 12/31/2018
Last Monday for 06-Jan-2019: 12/31/2018
Last Monday for 15-Jan-2019: 01/14/2019
Last Monday for 31-Jan-2019: 01/28/2019

Related

How do you find the first date of the week in SAS from a date?

I have a variable in a SAS dataset that has a number of dates (e.g. 01APR21). What I'm looking to do is create a new variable that shows the date of the first Monday of that week. So using the above example of 01APR21, the output would be 29/03/2021 as that what was when the Monday in that week was. I'm assuming it's using intnx, but I can't get my head around it.
data test;
format date date8.;
format first_day date10.;
date = '01APR21'd;
first_day = ?;
run;
INTNX Parameters:
Interval : WEEK
Increment: 0 (same week)
Alignment: Beginning
(Sunday)
Then add 1 to get to Monday instead of Sunday. You could probably play with the SHIFT INDEX parameter as well.
Monday = intnx('week', dateVariable, 0, 'B') + 1

JMeter: How to get date of last week starting from sunday till saturday?

The scenario is I need to get last week date in format yyyy-mm-dd HH:mm:ss from last Sunday till Saturday(date will increment by 1 from last week of Sunday till Saturday) and like wise one more is to get date from past 20 weeks(till last week).
How may I get it?
Your requirement is not very clear so I can provide only a generic solution:
Getting the "last Sunday"
def calendar = Calendar.instance
def delta = Calendar.SUNDAY - calendar.get(Calendar.DAY_OF_WEEK)
calendar.add(Calendar.DAY_OF_WEEK, delta)
log.info('Last Sunday: ' + calendar.time.format("yyyy-MM-dd HH:mm:ss"))
Getting all previous "dates" for last 20 weeks:
def now = new Date()
use(groovy.time.TimeCategory) {
def twentyWeeksAgo = now - 30.weeks
def duration = now - twentyWeeksAgo
1.upto(duration.days, {
twentyWeeksAgo = twentyWeeksAgo + 1.days
log.info(twentyWeeksAgo.format('yyyy-MM-dd HH:mm:ss'))
})
}
More information:
Groovy TimeCategory
Creating and Testing Dates in JMeter - Learn How

MS Access 2010 (Design View): return Monday of the current week with Monday as 1st day of the week

I need to make my Access query always return the Monday of the current week. I have seen a few solutions on Google/StackOverflow but they are written in SQL and I am a beginner in creating Access queries (I am using the Design view to make them).
Goal: The week should be considered as M T W T F S S. Then, the query should always return the Monday of the current week. Therefore, if it is Sunday, it should still return the Monday before, NOT the next week's Monday. Can anyone explain how to do this using the Design View in Access 2010?
Keep in mind that in this context we are working with dates, so if we do Date() - 1, we will get 1 day prior to today.
Date() ~ Today's date
DatePart(
"w" - Weekday
Date() - Today's date
2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
)
-1 - Subtract 1 from the DatePart value.
Values
Date() = 4/27/2015 (at time of this writing)
DatePart("w",Date(),2,1) = 1
DatePart("w",Date(),2,1)-1 = 0
So we have Date()-0... Okay, what's so great about that? Well, let's look at a more useful scenario where today's date is a day other than Monday.
Let's act like today is 4/28/2015 (Tuesday)
Date() = 4/28/2015
DatePart("w",Date(),2,1) = 2
DatePart("w",Date(),2,1)-1 = 1
So, from the outside, in; give me the current weekday value. (1 = Monday, 2 = Tuesday, etc.), and subtract 1 from that -> that's how many days we need to subtract from the current date to get back to the weekday value of 1 (Monday).
Here's a function that will do this:
Public Function DatePrevWeekday( _
ByVal datDate As Date, _
Optional ByVal bytWeekday As VbDayOfWeek = vbMonday) _
As Date
' Returns the date of the previous weekday, as spelled in vbXxxxday, prior to datDate.
' 2000-09-06. Cactus Data ApS.
' No special error handling.
On Error Resume Next
DatePrevWeekday = DateAdd("d", 1 - Weekday(datDate, bytWeekday), datDate)
End Function
As vbMonday is 2 and your date is today, you can use the core expression in a query:
PreviousMonday: DateAdd("d",1-Weekday(Date(),2),Date())

Finding last Sunday and going 4 weeks backward every week in SAS

I have a SAS job that runs every Thursday, but sometime it need to run on Wednesday, and maybe Tuesday evening. The job collects some data in 4 week intervals up until the closest Sunday. For example, today we have 19Mar2015, and I need data until 15Mar2015.
data get_some_data;
set all_the_data;
where date >= '16Feb2015' and date <= '15Mar2015';
run;
Next week I have to manually change the date parameters too
data get_some_data;
set all_the_data;
where date >= '23Feb2015' and date <= '22Mar2015';
run;
Anyway I can automate this?
I'll expand on the suggestion from #user667489 as it could take you a while to work it out. The key is to use the week time interval, which by default starts on a Sunday (you can change this with a shift index, read this for further details)
So your query just needs to be :
where intnx('week',today(),-4)<date<=intnx('week',today(),0);
Use the INTNX function to regress the date back to last Sunday:
data get_some_data;
set all_the_data;
lastsun=intnx('week',today(),0);
/*where date >= '23Feb2015' and date <= '22Mar2015';*/
where date between lastsun-27 and lastsun;
run;
You can try getting the last sunday date using weekday function and then using INTNX get the 4 week back date from that sunday date. Check the below ref code :
data mydata;
input input_date YYMMDD10.;
/* Loop to get the last sunday date, do the processing
and get out of loop */
do i =0 to 7 until(last_sunday_date>0);
/* Weekday Sunday=1 */
if weekday(sum(input_date,-i))=1 then do;
last_sunday_date=sum(input_date,-i);
/* INTNX to get the 4 week back date */
my_4_week_start=intnx('week',last_sunday_date,-4);
end;
end;
format input_date last_sunday_date my_4_week_start yymmdd10.;
datalines4;
2015-03-01
2015-03-07
2015-03-14
2015-03-21
2015-03-28
2015-04-05
2015-04-13
2015-04-20
;;;;
run;
proc print data=mydata;run;
let me know if this helps!

Rails convert datetime to date inwhere()

I've got the following problem:
i need to convert a datetime field in a where select to a date field.
scope :after_start_date, lambda { |value| where('end_time <= (?)', value) if value}
I need something like where(end_time.to_date + " >= (?)", value)
Is there a nice way to do this?
Update/Example
08.09.2014 14:40 as DB_END_TIME
08.09.2014 AS PARAM_END_TIME
I wanna get all results which until the date 08.09.2014 (including the 8th),
but the comparison where(DB_END_TIME <= PARAM_END_TIME) does not include 08.09.2014
because the PARAM_END_TIME has 00:00:00 as time value.
And i think its not nice to do something like this:
PARAM_END_TIME + 1.day - 1.second - this would work.
So i'd like to convert the database value to the date format in the where statement.
you can use ruby strftime method to create ur own date/time display.
time.strftime( string ) => string
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"
Example:
date = Date.parse('2014-09-08 00:00:00 UTC')
# => Mon, 08 Sep 2014
date.strftime('%d-%m-%Y')
# => "08-09-2014"
And search in database accordingly.. I hope this may helps you
Edit:
If you want to fetch data from your SQL Server and convert datetime to date then have a look at this