How do I get the bash date script to return a day of the week relative to a non-current time?
You can use a little day number arithmetic:
base="02/1/2012"
feb1_dayofweek=$( date -d $base +%w )
target_dayofweek=0 # sunday
date -d "$base - $(( (7 + feb1_dayofweek - target_dayofweek) % 7 )) days"
result:
Sun Jan 29 00:00:00 EST 2012
Get the date (a day before current time) in Bash
Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.
I find out I can get the previous date with little trick on timezone.
DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE
Get Day Of Week in bash script
Use %u
. Like this:
DOW=$(date +%u)
From the man page:
%u day of week (1..7); 1 is Monday
How do I get the first day of the given year's day of the week in Bash Shell?
Please see the below code which will give you 1 first day of the year passed:
# !/usr/bin/bash
year=2020
firstday=`date -d "01 Jan $year" +'%A,%d'`
echo "First day of the year '$year' is : '$firstday'"
Output:
First day of the year '2020' is : 'Wednesday,01'
If you want to just get the day , then remove the %d option.
firstday=`date -d "01 Jan $year" +'%A`
Output :First day of the year '2020' is : 'Wednesday'
How to increment datetime by custom months in python without using library
Edit - based on your comment of dates being needed to be rounded down if there are fewer days in the next month, here is a solution:
import datetime
import calendar
def add_months(sourcedate, months):
month = sourcedate.month - 1 + months
year = sourcedate.year + month // 12
month = month % 12 + 1
day = min(sourcedate.day, calendar.monthrange(year,month)[1])
return datetime.date(year, month, day)
In use:
>>> somedate = datetime.date.today()
>>> somedate
datetime.date(2010, 11, 9)
>>> add_months(somedate,1)
datetime.date(2010, 12, 9)
>>> add_months(somedate,23)
datetime.date(2012, 10, 9)
>>> otherdate = datetime.date(2010,10,31)
>>> add_months(otherdate,1)
datetime.date(2010, 11, 30)
Also, if you're not worried about hours, minutes and seconds you could use date
rather than datetime
. If you are worried about hours, minutes and seconds you need to modify my code to use datetime
and copy hours, minutes and seconds from the source to the result.
execution_date in airflow: need to access as a variable
The BashOperator
's bash_command
argument is a template. You can access execution_date
in any template as a datetime
object using the execution_date
variable. In the template, you can use any jinja2
methods to manipulate it.
Using the following as your BashOperator
bash_command
string:
# pass in the first of the current month
some_command.sh {{ execution_date.replace(day=1) }}
# last day of previous month
some_command.sh {{ execution_date.replace(day=1) - macros.timedelta(days=1) }}
If you just want the string equivalent of the execution date, ds
will return a datestamp (YYYY-MM-DD), ds_nodash
returns same without dashes (YYYYMMDD), etc. More on macros
is available in the Api Docs.
Your final operator would look like:
command = """curl -XPOST '%(hostname)s:8000/run?st={{ ds }}'""" % locals()
t1 = BashOperator( task_id='rest-api-1', bash_command=command, dag=dag)
Python - Get Yesterday's date as a string in YYYY-MM-DD format
You Just need to subtract one day from today's date. In Python datetime.timedelta
object lets you create specific spans of time as a timedelta
object.
datetime.timedelta(1)
gives you the duration of "one day" and is subtractable from a datetime
object. After you subtracted the objects you can use datetime.strftime
in order to convert the result --which is a date object-- to string format based on your format of choice:
>>> from datetime import datetime, timedelta
>>> yesterday = datetime.now() - timedelta(1)
>>> type(yesterday)
>>> datetime.datetime
>>> datetime.strftime(yesterday, '%Y-%m-%d')
'2015-05-26'
Note that instead of calling the datetime.strftime
function, you can also directly use strftime
method of datetime
objects:
>>> (datetime.now() - timedelta(1)).strftime('%Y-%m-%d')
'2015-05-26'
As a function:
from datetime import datetime, timedelta
def yesterday(frmt='%Y-%m-%d', string=True):
yesterday = datetime.now() - timedelta(1)
if string:
return yesterday.strftime(frmt)
return yesterday
example:
In [10]: yesterday()
Out[10]: '2022-05-13'
In [11]: yesterday(string=False)
Out[11]: datetime.datetime(2022, 5, 13, 12, 34, 31, 701270)
Related Topics
Get Unique Serial Number of Usb Device Mounted to /Dev Folder
How to Get Maximum Tcp Receive/Send Window in MAC Os X
How to Convert Dynamically Linked Application to Statically One
Distro for Linux Kernel Development
Running a Script Just Before Installation of Debian Finishes Using Preseed
Vagrant Ssh -C and Keeping a Background Process Running After Connection Closed
In Linux, Physical Memory Pages Belong to The Kernel Data Segment Are Swappable or Not
How to Provide Extend-On-Write Functionality for Memory Mapped Files in Linux
How Does Linux Support More Than 512Gb of Virtual Address Range in X86-64
Understanding /Boot/Config File
Reusing Custom Makefile for Static Library with Cmake
Stdin into Zip Command, How to Specify a File Name
Linux Append Console Output to a Logfile