datetime - How to strip just date and validate the date - selenium

from selenium import webdriver
import time
from datetime import datetime
driver = webdriver.Chrome(executable_path ="C:/ChromeDriver/chromedriver.exe")
driver.maximize_window()
driver.get("http://www.timestampconvert.com")
driver.implicitly_wait(2)
time.sleep(2)
date_displayed = driver.find_element_by_xpath('/html/body/div[4]/div[2]/form/div/div[3]/div/span[5]')
print('Date displayed on page:', date_displayed.text)
try:
datetime.strptime(date_displayed.text, '%d-%m-%Y %H:%M:%S')
print('The date format {} is valid.'.format(date_displayed.text))
except ValueError:
print('The date {} is invalid'.format(date_displayed.text))
The above code executes fine BUT It doesn't do what I am expecting
1. I just want the date to be displayed and strip time, but it displays both date and time?
2. How do i validate the date and month?
For example
If date is displayed as 24/05/2020 - It should pass because expected date format is DD-MM-YYYY
If date is displayed as 06/16/2020 - It should Fail because expected date format is DD-MM-YYYY but actual format displayed is MM/DD/YYYY
--
I have a requirements to test specific date formats on my applications and i want to specify
first digit is always 'DD' and it range from 1 to 31
second digit is always 'MM' and it range from 1 to 12
it should validate and should pass / fail the test accordingly.
Any suggestions please?

why not
try:
mydate=date_display.test.split(" ")[0]
datetime.strptime(mydate, '%d-%m-%Y')
print('The date format {} is valid.'.format(mydate))
except ValueError:
print('The date {} is invalid'.format(mydate))

Related

AWS Glue studio converting Pyspark string column to date returns null

I have data from an S3 bucket and want to convert the Date column from string to date. The current Date column is in the format 7/1/2022 12:0:15 AM.
Current code I am using in AWS Glue Studio to attempt the custom transformation:
MyTransform (glueContext, dfc) -> DynamicFrameCollection:
from pyspark.sql.functions import col, to_timestamp
df = dfc.select(list(dfc.keys())[0]).toDF()
df = df.withColumn('Date',to_timestamp(col("Date"), 'MM/dd/yyyy HH:MM:SS'))
df_res = DynamicFrame.fromDF(df, glueContext, "df")
return(DynamicFrameCollection({"CustomTransform0": df_res}, glueContext))
With MM/dd/yyyy HH:MM:SS date formatting, it runs but returns null for the Date column. When I try any other date format besides this, it errors out. I suspect the date formatting may be the issue, but I am not certain.
After converting string to timestamp you need to cast it to date type, like this:
df = df.withColumn(df_col, df[df_col].cast("date"))
We ended up removing the HH:MM:SS portion of the date format and this worked for our needs. I would still be interested if anyone can figure out how to get the hours, minutes, seconds, and AM/PM to work, but we can do without for now.

Outputting pandas timestamp to tuple with just month and day

I have a pandas dataframe with a timestamp field which I have successfully to converted to datetime format and now I want to output just the month and day as a tuple for the first date value in the data frame. It is for a test and the output must not have leading zeros. I ahve tried a number of things but I cannot find an answer without converting the timestamp to a string which does not work.
This is the format
2021-05-04 14:20:00.426577
df_cleaned['trans_timestamp']=pd.to_datetime(df_cleaned['trans_timestamp']) is as far as I have got with the code.
I have been working on this for days and cannot get output the checker will accept.
Update
If you want to extract month and day from the first record (solution proposed by #FObersteiner)
>>> df['trans_timestamp'].iloc[0].timetuple()[1:3]
(5, 4)
If you want extract all month and day from your dataframe, use:
# Setup
df = pd.DataFrame({'trans_timestamp': ['2021-05-04 14:20:00.426577']})
df['trans_timestamp'] = pd.to_datetime(df['trans_timestamp'])
# Extract tuple
df['month_day'] = df['trans_timestamp'].apply(lambda x: (x.month, x.day))
print(df)
# Output
trans_timestamp month_day
0 2021-05-04 14:20:00.426577 (5, 4)

How do I convert a non zero padded day string to a useful date in pandas

I'm trying to import a date string with non-zero padded day, zero padded month, and year without century to create a datetime e.g. (11219 to 01/12/19). However, pandas cannot distinguish between the day and the month (e.g. 11219 could be 11th February, 2019 or 1st December, 2019).
I've tried using 'dayfirst' and the '#' in the day e.g. %#d, but nothing works. Code below, any advise?
Code:
df_import['newDate'] = pd.to_datetime(df_import['Date'], format='%d/%m/%Y', dayfirst = True)
Error:
time data '11219' does not match format '%d/%m/%Y' (match)
Since only the day is not zero-padded, the dates are unambiguous. They can simply be parsed by Pandas if we add the pad:
pd.to_datetime(df_import['Date'].str.zfill(6), format='%d%m%y')
use zfill()
A custom function can also be used if you want to handle more cases.
def getDate(str):
return #logic to parse
df_import['newDate'] = df_import['Date'].apply(getDate)

Datetime issue in odoo10?

in my custom model I defined the fields
time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
tot_time = fields.Char("Time Difference", compute='_get_time')
this is my compute function
#api.depends('time_from', 'time_to')
def _get_time(self):
t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
if t2<t1:
raise ValidationError('Time To must greater than Time From')
time_diff = (t2-t1)
self.tot_time = time_diff
This is success fully prints time difference.
Time from and time to are mm/dd/yyyy hh:mm:ss format.
How to change this to mm/dd/yyyy hh:mm:ss format
I changed the format like this '%Y-%d-%m %H:%M:%S' .
but it is not getting correct result.
How to change the format?
is it possible to calculate time difference in this format?
The Date and Datetime are saved as strings in the database in a certain format (see the class definition on fields.py. You cannot change the format that is used for the fields in the ORM. If you want to change the format of the date or datetime when you show these fields you can do it not from the code but from:
1) Settings -> Translations -> Find your language and inside you can change the way the fields Date and Datetime are rendered on the client side.
2) If you have a template/report you can use for example<p t-esc="formatLang(time.strftime('%Y-%m-%d %H:%M:%S')" /> or another expression you want to change how the date or datetime will be formed.
3) In the field definition in your xml files you can use custom javascript/widget that will do the rendering.

trying to format pandas.to_datetime

I'm trying to get today's date in a few different formats and I keep getting errors:
pd.to_datetime('Today',format='%m/%d/%Y') + MonthEnd(-1)
ValueError: time data 'Today' does not match format '%m/%d/%Y' (match)
What is the correct syntax to get todays date in yyyy-mm-dd and yyyymm formats?
For YYYY-MM-DD format, you can do this:
import datetime as dt
print(dt.datetime.today().date())
2017-05-23
For YYYY-MM format, you can do this:
print(dt.datetime.today().date().strftime('%Y-%m'))
2017-05
If you need to do this on just a few columns you can use:
import pandas as pd
dataframe_name['Date_Column_name'].apply(pd.tslib.normalize_date)
This method doesn't use any other module except pandas. If you need a "custom" date format you can always do:
from datetime import datetime as dt
dataframe_name['Date_Column_name'].dt.strftime('%d/%m/%Y')
Here is a list of strftime options.