Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hex to int convertion
#1
Hi,

Why isn't the date printed correctly?

from datetime import datetime as dt

now = dt.now()

print(hex(now.second)) #return 0x0
print(hex(now.minute)) #return 0x16
print(hex(now.hour)) #return 0x15
print(hex(now.weekday())) #return 0x0
print(hex(now.day)) #return 0x6
print(hex(now.month)) #return 0x7
print(hex(now.year-2000)) #returns 0x14

# actual datetime = 2020 7 6 21 22 0 0(weekday)

# sec min hour weekday day month year(2 digit)
t = [0x0, 0x16, 0x15, 0x0, 0x6, 0x7, 0x14]
w = ["Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"]

print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]+1]))
#converting to int no change
print("20%x/%02x/%02x %02x:%02x:%02x %s" %(int(t[6]),int(t[5]),int(t[4]),int(t[2]),int(t[1]),int(t[0]),w[int(t[3])+1]))
Reply
#2
Here is one way to do it
#! /usr/bin/env python3

from datetime import datetime as dt
now = dt.now()
time_arr = [now.year, now.month, now.day, now.hour, now.minute, now.second]
hex_time = []
for item in time_arr:
    hex_time.append(hex(item))
    
print(hex_time)
print(' '.join(hex_time))
Output:
['0x7e4', '0x7', '0x6', '0xf', '0xd', '0x9'] 0x7e4 0x7 0x6 0xf 0xd 0x9
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Partial convertion string to int in lists satellite89 6 3,365 Apr-22-2019, 08:50 PM
Last Post: Yoriz
  Looking for csv to json convertion script rajaniyer123 1 2,839 Jul-06-2017, 10:42 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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