Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
date time question
#1
I would like to convert string in to date format which is acceptable by elasticsearch

doc = {
'submitted': '2020-01-25 20:10:10',
'num': '2',
'timestamp': 1579624522874,
}

I have two field submitted and timestamp
timestamp I was able to convert using following and elasticsearch took it as timestamp.
datetime.fromtimestamp(int(doc['timestamp'])/1000).strftime('%Y-%m-%d %H:%M:%S')


but submitted I have to convert in to isoformat which I can't seems to get it working.
I also want to add timezone "Etc/UTC" to it.
Reply
#2
Use code tags.
(Jan-31-2020, 06:11 PM)pythonlearner1 Wrote: I also want to add timezone "Etc/UTC" to it.
If timezone is involved i now use only Pendulum,before it often was a combination of datetime, dateutil, pytz ..ect Doh
>>> import pendulum
>>> 
>>> d = doc.get('submitted')
>>> d
'2020-01-25 20:10:10'
>>> pendulum.parse(d)
DateTime(2020, 1, 25, 20, 10, 10, tzinfo=Timezone('UTC'))
>>> print(pendulum.parse(d))
2020-01-25T20:10:10+00:00
>>> 
>>> ts = doc.get('timestamp')
>>> ts
1579624522874
>>> ts = ts / 1000
>>> pendulum.from_timestamp(ts)
DateTime(2020, 1, 21, 16, 35, 22, 874000, tzinfo=Timezone('UTC'))
>>> print(pendulum.from_timestamp(ts))
2020-01-21T16:35:22.874000+00:00
Reply
#3
this is good. it is working
but I want to keep date and time 20:10:10 as is and then tell it that this is UTC timezone.
I just have play around with different setting.

as I want date-time as is and send it to elasticsearch. and then elasticsearch should save it as utc. and kibana will convert it back to local time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 126 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 314 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,162 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  Wait til a date and time KatManDEW 2 1,390 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  Date format and past date check function Turtle 5 4,076 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,191 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Naming the file as time and date. BettyTurnips 3 2,891 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Update Date based on Time/String stevezemlicka 1 1,988 Jan-08-2021, 06:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020