Python Forum
[SOLVED] Alternative to regex to extract date from whole timestamp?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Alternative to regex to extract date from whole timestamp?
#1
Hello,

Python being such a rich language, I was wondering if it provides a ready-to-use function to extract juste the date from a whole timestamp instead of using a regular expression:

#Thu, 10 Nov 2022 18:15:41 +0000 pattern_pubDate = re.compile('(\d{2}) (\w+?) (\d{4})') … m = pattern_pubDate.search(link.pubDate.string) if m:	pubDate = m.group(0)
I'm only interested in the date.

Thank you.
Reply
#2
I put this together, for my own reference, but if it's of help, then you're welcome to it.

import time hr = '{}{}{}'.format('\n'*2,'-'*50,'\n') #horizontal rule print('''time.gmtime(0): returns system’s epoch setting: The starting point against which you can measure the passage of time. ''') system_epoch = time.gmtime() print(system_epoch,hr) print('''time.time() returns the number of seconds that have passed since the epoch; return value is a floating point number to account for fractional seconds: ''') tt = time.time() print(tt,hr) print('''time.ctime() will return a string object representation of the time and date based on the number of seconds since epoch, or the current time and date, if nothing is passed to the function. The string representation of time, also known as a timestamp, returned by ctime() is formatted with the following structure: 1: Day of the week 2: Month of the year 3: Day of the month 4: Hours, minutes, and seconds using the 24-hour clock notation 5: Year ''') tc = time.ctime(1660867200) print('''Note: time.ctime(0) may not return the same timestamp on every computer system ''') print('''For details about time zones, see: https://realpython.com/python-time-module/ ''') print(tc,hr)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Thank you.
Reply
#4
You can parse it as a string into a datetime object using strptime() than you can manipulate it as you want:

import datetime as dt string_date = "Thu, 10 Nov 2022 18:15:41 +0000" date_obj = dt.datetime.strptime(string_date, "%a, %d %b %Y %H:%M:%S %z") print(date_obj.date()) print(date_obj.day) print(date_obj.hour) print(date_obj.weekday())
Output:
date: 2022-11-10 day 10 hour 18 weekday: 3
Source
snippsat likes this post
Reply
#5
Brilliant!
Reply
#6
A exmaple with Pendulum which can parse the date direclty.
One of the strong part is that special care has been taken to ensure timezones are handled correctly.
>>> import pendulum >>> >>> date = "Thu, 10 Nov 2022 18:15:41 +0000" >>> date = pendulum.parse(date, strict=False) >>> date DateTime(2022, 11, 10, 18, 15, 41, tzinfo=Timezone('UTC')) >>> print(date) 2022-11-10T18:15:41+00:00 >>> # How long ago for fun >>> pendulum.now().subtract(days=date.day).diff_for_humans() '1 week ago'
carecavoador and Winfried like this post
Reply
#7
(Nov-16-2022, 01:48 PM)snippsat Wrote: A exmaple with Pendulum which can parse the date direclty.
One of the strong part is that special care has been taken to ensure timezones are handled correctly.

Wow, this is a nice package I didn't know about. Very nice!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] regex issue paul18fr 3 554 Oct-19-2025, 11:21 PM
Last Post: Pedroski55
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,850 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
Question [SOLVED] [Windows] Change directory date? Winfried 2 1,413 May-11-2025, 04:24 PM
Last Post: Winfried
Question [SOLVED] Can't figure out right regex Winfried 3 1,644 Mar-02-2025, 05:57 PM
Last Post: Winfried
  Compare current date on calendar with date format file name Fioravanti 1 3,280 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 4,277 Jan-20-2024, 04:45 AM
Last Post: 1418
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 7,148 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  Help with a regex? (solved) wrybread 3 2,150 May-01-2023, 05:12 AM
Last Post: deanhystad
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 2,796 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Epoch timestamp without milliseconds? Winfried 5 8,867 Jan-27-2023, 04:35 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.