Convert string "YYYYMMDDHHMMSS" to timestamp in Python [duplicate] - python

This question already has an answer here:
Python format date using only string format() method
(1 answer)
Closed 3 years ago.
I need help to convert this string '20190625091115' to timestamp '25-06-2019 09:11:15' in python. Format in 'YYYYMMDDHHMMSS > Format out 'DD-MM-YYYY HH:MM:SS'.

First part would be creating datetime object:
from datetime import datetime
date_string = "20190625091115"
format_date = datetime.strptime(date_string, '%Y%m%d%H%M%S'))
After which format date is:
print(format_date)
2019-06-25 09:11:15
Little clarification here, on python reference page, you can see definition for '%Y%m%d%H%M%S') format specifiers I used.
%Y: Year with century as a decimal number, e.g. 1970, 1988, 2001, 2013
%m: Month as a zero-padded decimal number (e.g. 01, 02, ..., 12)
%d: Day of the month as a zero-padded decimal number (e.g. 01, 02, ..., 31)
%H: Hour (24-hour clock) as a zero-padded decimal number (e.g 00, 01, ..., 23)
%M: Minute as a zero-padded decimal number (e.g 00, 01, ..., 59)
%S: Second as a zero-padded decimal number (e.g. 00, 01, ..., 59)
%f: Microsecond as a decimal number, zero-padded on the left (000000, 000001, ..., 999999)
%z: UTC offset in the form +HHMM or -HHMM, empty string if the the object is naive, (empty or +0000, -0400, +1030)

Related

Python datetime minus 1 day using timedelta

I would like the first datetime in the hyperlink to be 1 day before the second which is today's date. I read a little bit about the timedelta but I did not see how it applied within a hyperlink.
http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=skatersummary&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%22' + datetime.datetime.now().strftime('%Y-%m-%d') + '%22%20and%20gameDate%3C=%22' + datetime.datetime.now().strftime('%Y-%m-%d') + '%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22
Some well placed parentheses should suffice:
(datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
A friendly suggestion: take a look at Python string formatting instead of constructing your string using concatenation. It'll end up being a lot cleaner and less repetitive.
I needed to label files with a header that was 1 day prior to execution so this worked for me:
from datetime import date, timedelta, datetime
header = (datetime.now()-timedelta(days=1)).strftime("%Y-%m-%d")
so that "header" became my string
For class datetime.timedelta the valid arguments are:
days=0
seconds=0
microseconds=0
milliseconds=0
minutes=0
hours=0
weeks=0
All arguments are optional and default to 0. Arguments may be integers or floats, and may be positive or negative. Only days, seconds and microseconds are stored internally. Arguments are converted to those units:
A millisecond is converted to 1000 microseconds.
A minute is converted to 60 seconds.
An hour is converted to 3600 seconds.
A week is converted to 7 days.
For strftime()
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a decimal number. (Platform specific) 30
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. September
%m Month as a zero-padded decimal number. 09
%-m Month as a decimal number. (Platform specific) 9
%y Year without century as a zero-padded decimal number. 13
%Y Year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 07
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 7
%I Hour (12-hour clock) as a zero-padded decimal number. 07
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 000000
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z Time zone name (empty string if the object is naive).
%j Day of the year as a zero-padded decimal number. 273
%-j Day of the year as a decimal number. (Platform specific) 273
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 39
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 39
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 07:06:05
%% A literal '%' character. %

Python date format D M d H:i:s T Y

I have a datetime object,
import time, datetime, pytz
current_unixtime = time.time()
current_date_milis_for_blibli = int(round(current_unixtime * 1000))
current_datetime_object = datetime.datetime.fromtimestamp(current_unixtime, pytz.timezone('Asia/Jakarta'))
how do i convert it into:
Mon May 16 14:07:15 WIB 2016
or in PHP equivalence:
D M d H:i:s T Y
What i tried are written below, as you can see, i can't seem to get the 3 characters for Day and Month:
year = current_datetime_object.year
month = current_datetime_object.month
day = current_datetime_object.day
hour = current_datetime_object.hour
minute = current_datetime_object.minute
second = current_datetime_object.second
result = current_datetime_object.strftime("%a %b %d %H:%M:%S %Z %Y")
You can also specify output by changing values in brackets.
Examples are based on datetime.datetime(2013, 9, 30, 7, 6, 5).
Code Example Meaning
%a Mon # Weekday as locale’s abbreviated name.
%A Monday # Weekday as locale’s full name.
%w 1 # Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
%d 30 # Day of the month as a zero-padded decimal number.
%-d 30 # Day of the month as a decimal number. (Platform specific)
%b Sep # Month as locale’s abbreviated name.
%B September # Month as locale’s full name.
%m 9 # Month as a zero-padded decimal number.
%-m 9 # Month as a decimal number. (Platform specific)
%y 13 # Year without century as a zero-padded decimal number.
%Y 2013 # Year with century as a decimal number.
%H 7 # Hour (24-hour clock) as a zero-padded decimal number.
%-H 7 # Hour (24-hour clock) as a decimal number. (Platform specific)
%I 7 # Hour (12-hour clock) as a zero-padded decimal number.
%-I 7 # Hour (12-hour clock) as a decimal number. (Platform specific)
%p AM # Locale’s equivalent of either AM or PM.
%M 6 # Minute as a zero-padded decimal number.
%-M 6 # Minute as a decimal number. (Platform specific)
%S 5 # Second as a zero-padded decimal number.
%-S 5 # Second as a decimal number. (Platform specific)
%f 0 # Microsecond as a decimal number, zero-padded on the left.
%z # UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z # Time zone name (empty string if the object is naive).
%j 273 # Day of the year as a zero-padded decimal number.
%-j 273 # Day of the year as a decimal number. (Platform specific)
%U 39 # Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
%W 39 # Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
%c Mon Sep 30 07:06:05 2013 # Locale’s appropriate date and time representation.
%x 09/30/13 # Locale’s appropriate date representation.
%X 07:06:05 # Locale’s appropriate time representation.
%% % # A literal '%' character.
Example is taken from here
Use the datetime module:
import datetime
datetime.datetime.now().strftime('%a %B %d %H:%M:%S %Z %Y')
I can find the answer to the question here, turns out Python Documentation mentioned this kind of conversion between datetime object to a formatted string:
https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Format of "2016-10-22T16:27:54+0000" to convert to datetime [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Cannot parse the date in Python
(3 answers)
Closed 6 years ago.
I want to convert a string to datetime.
The string is:
date_created = "2016-10-22T16:27:54+0000"
And I am trying to convert that with:
datetime.strptime(date_created, '%y-%m-%dT%H:%M:%S+0000')
but the format does not match.
So, what is the correct format to use?
%y matches a year with two digits, but your input uses 4 digits. Use %Y instead:
>>> from datetime import datetime
>>> date_created = "2016-10-22T16:27:54+0000"
>>> datetime.strptime(date_created, '%Y-%m-%dT%H:%M:%S+0000')
datetime.datetime(2016, 10, 22, 16, 27, 54)
From the strftime() and strptime() Behavior section:
%y
Year without century as a zero-padded decimal number.
00, 01, ..., 99
%Y
Year with century as a decimal number.
0001, 0002, ..., 2013, 2014, ..., 9998, 9999

Python strptime returns ValueError for apparent valid format

The following date is returning a value error for what appears to be a valid datetime string. Why?
from datetime import datetime
dateFormat = "%A %b %d, %Y %I:%M %p"
myDateStr = "Sunday May 22, 2016 00:47 AM"
try:
date_object = datetime.strptime(myDateStr,dateFormat)
print(date_object)
except ValueError as e:
print(e)
I will admit to being slightly confused. It's been tested on two platforms and does generally work.
Thanks
For the benefit of the reader here are the format masks.
%A Weekday as locale’s full name. Sunday, Monday, ..., Saturday (en_US);
%b (%B) Month as locale’s abbreviated name. Jan, Feb, ..., Dec (en_US);
%d Day of the month as a zero-padded decimal number, 01, 02, ..., 31
%Y Year with century as a decimal number. 1970, 1988, 2001, 2013
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
%M Minute as a zero-padded decimal number. 00, 01, ..., 59
%p Locale’s equivalent of either AM or PM. AM, PM (en_US);
You are using %I which as you said is 12-hour format, yet you use 24-hour format in your string, ie 00:47.
00:47 AM is not a valid time specification in any format.
Changing 00:47 AM to 12:47 AM, or %I to %H, fixes this issue.

How to convert the time now into a custom data format? [duplicate]

This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 6 years ago.
I am using an API, and the API needs this data format:
Wed Jan 07 2015 18:58:40
How I can convert the time now to this data format, using the datetime and time modules?
print(datetime.now().strftime('%a %b %d %Y %H:%M:%S'))
Would display something like:
Thu Mar 24 2016 10:09:18
The formatting options used are as follows:
%a Weekday as locale’s abbreviated name.
%b Month as locale’s abbreviated name.
%d Day of the month as a zero-padded decimal number.
%Y Year with century as a decimal number.
%H Hour (24-hour clock) as a zero-padded decimal number.
%M Minute as a zero-padded decimal number.
%S Second as a zero-padded decimal number.
To then convert this to a format for sending, you probably want to investigate quote_plus(), for example:
from datetime import datetime
import urllib
now = datetime.now().strftime('%a %b %d %Y %H:%M:%S')
print(urllib.parse.quote_plus(now))
This would give you:
Thu+Mar+24+2016+10%3A32%3A51

Categories

Resources