Answers for "python add datetime"

2

add time to datetime python

add_time = datetime.timedelta(days=2)
Posted by: Guest on June-13-2021
1

add time delta pytohn

import datetime

ts = ts + datetime.timedelta(seconds=1)
ts = ts + datetime.timedelta(minutes=1)
ts = ts + datetime.timedelta(hours=1)
ts = ts + datetime.timedelta(days=1)
Posted by: Guest on June-26-2020
0

python insert datetime

from datetime import datetime

import mysql.connector

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')

    mySql_insert_query = """INSERT INTO Laptop (Id, Name, Price, Purchase_date) 
                            VALUES (%s, %s, %s, %s) """

    cursor = connection.cursor()
    current_Date = datetime.now()
    # convert date in the format you want
    formatted_date = current_Date.strftime('%Y-%m-%d %H:%M:%S')
    insert_tuple = (7, 'Acer Predator Triton', 2435, current_Date)

    result = cursor.execute(mySql_insert_query, insert_tuple)
    connection.commit()
    print("Date Record inserted successfully")

except mysql.connector.Error as error:
    connection.rollback()
    print("Failed to insert into MySQL table {}".format(error))

finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Posted by: Guest on May-17-2021

Python Answers by Framework

Browse Popular Code Answers by Language