Datetime Methods

Datetime Methods

The sorted() Method:

- The sorted() method uses to put the earlier string date in the first order, and the later string date in the second order.

- In the below code, we take three dates in the dates list, we can take more dates into a list.



The replace() Method:

- The replace() method replaces a specific date or time with a new date or time.

- In the below code, we changed the month and minute in the replace_dt variable.



The fromtimestamp() Method:

- Timestamp: Number of seconds.

- Python will read timestamps and return a datetime.

- We call the fromtimestamp() method to the datetime module in the print function.



The isoformat() 8601 Method:

- The isoformat() 8601 method returns the date and time format as iso format.

- Return ISO Format for a date (YYYY-MM-DD)

- Return ISO Format for datetime (YYYY-MM-DDTHH:MM:SS)

- In ISO Format, the date requires 3 parameters, and datetime requires 6 parameters.



The strftime() Method:

- The strftime() method turn dates into a string.

- The strftime() method required one parameter.

- %Y Code,  means return the Year in Full Form of the given date. 
- %m Code, means return the month of the given date. 
- %d Code,  means return the day of the given date.

- To use the above codes put the code in the string in the strftime method
(See in the below Code)



The strptime() Method:

- The strptime() method requires a datetime object and returns a string representation of the datetime.

The strptime() method takes two arguments:

1st. Datetime in String.

2nd. String Format that says how We represent the datetime.

- We need an exact match to do a string conversion.


Comments