String date manipulation in Python

In Python, I often want to receive the date etc. as a character string and process it, but since I google it every time, I will summarize it for myself. In my case, it is processed by selecting character string → datetime → character string.

Premise

Receives the date as a string. For example, I think there are the following cases.

  1. 2014-10-16
  2. 2014/10/16 20:29:39

It's a story of what to do if you want these one day later.

String → datetime

First parse the string.

import datetime

#In case of 1
date1 = '2014-10-16'
d = datetime.datetime.strptime(date1, '%Y-%m-%d')

#In case of 2
date2 = '2014/10/16 20:29:39'
d = datetime.datetime.strptime(date2, '%Y/%m/%d %H:%M:%S')

This will convert it to datetime. Other formats can be supported by changing the above % Y-% m-% d.

datetime processing

Calculate datetime using timedelta. I want one day later, so

d += datetime.timedelta(days = 1)

If you do, you will get one day later. If you pull it without adding it, it will be one day ago, and if you change days to hours, you can control the time instead of the date.

datetime → string

Finally, return datetime to a string.

date_stringj = d.strftime('%Y-%m-%d')

This will make it a string in the specified format.

Summary

Below is a program that puts these together, receives the date as a character string, and adds one day.

add_1day.py


#!/usr/bin/python
import datetime

date_string_input = '2014-10-31'
date_format = '%Y-%m-%d'

#String → datetime
d = datetime.datetime.strptime(date_string_input, date_format)
#datetime processing
d += datetime.timedelta(days = 1)
#datetime → string
date_string_output = d.strftime(date_format)

print date_string_output

When executed, it will be as follows.

$ python time.py
2014-11-01

Recommended Posts

String date manipulation in Python
String manipulation in python
Date manipulation in Python
Conversion of string <-> date (date, datetime) in Python
Python string manipulation master
[Python2] Date string-> UnixTime-> Date string
Get date in Python
Date calculation in python
Date calculation in Python
[python] Convert date to string
Sort by date in python
Convert date timezone (time difference) in Python (from string)
Pixel manipulation of images in Python
File / folder path manipulation in Python
6 ways to string objects in Python
Create a random string in Python
Quadtree in Python --2
Python in optimization
CURL in python
Python: String concatenation
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Python date arithmetic
Python Grammar-String Manipulation
python string slice
Meta-analysis in Python
Unittest in python
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python string format
Constant in python
Python # string type
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
Python string inversion