Home > Back-end >  Convert datetime format data to minutes with Python (Data obtained from PowerBI) [closed]
Convert datetime format data to minutes with Python (Data obtained from PowerBI) [closed]

Time:10-07

I'm trying to convert datetime format to minutes in Python, but I don't have minutes in my datetime format.
I got data from PowerBI.
The problem is, showing datetime should be 420 minutes and I don't know how can I transform it.
Other data about totalminutes is right, only happens with some values.
I tried with his part of code but I only have 0 in minutes show i can't get anything.
date["day"] = date["day"].dt.day

1901-02-23 00:00:00

totalminutes   31398 non-null  object

array([datetime.datetime(1901, 2, 23, 0, 0),
   datetime.datetime(1900, 10, 26, 0, 0), 300, 360, 480, 240, 120,
   180, 600, 540, 720, 60, 0, 840, 660, 150, 570, 90, 210, 750, 30,
   630, 690, 510, 270, 330, 390, 450, 375, 435, 75, 315, 15, 255, 135,
   495, 555, 195, 245, 345, 525, 585, 290, 242, 254, 243, 244, 325,
   496, 165, 155, 415, 405, 620, 465, 105, 119, 303, 350, 220, 45,
   285, 231, 235, 222, 440, 221, 430, 445, 238, 239, 228, 229, 236,
   460, 425, 225, 455, 260, 230, 310, 305, 320, 280, 340, 500, 385,
   400, 409, 410, 370, 380, 830, 835, 520, 855, 675, 366, 850, 820,
   378, 795, 825, 432, 452, 470, 423, 431, 446, 463, 436, 438, 439,
   110, 355, 475, 530, 433, 365, 615, 395, 490, 485, 200, 140, 420,
   780, 515, 185, 134, 505, 509, 393, 467, 483, 565, 295, 437, 361,
   154, 130, 100, 457, 448, 426, 473, 348, 265, 670, 275, 250, 535,
   335, 454, 443, 389, 550, 575, 560, 458, 417, 456, 453, 447, 408,
   421, 383, 434, 428, 427, 810, 860, 845, 765, 730, 865, 930, 735,
   494, 359, 422, 590, 466, 502, 388, 441, 605, 418, 545, 442, 149,
   170, 645, 249, 271, 232, 241, 257, 251, 635, 314, 595, 650, 419,
   790, 705, 539, 479, 404, -15, 374, 899, 785, 131, 215, -16, 364,
   599, 569, 190, 779, 644, 664, 614, 694, 299, 482, 639, 674, 654,
   661, 537, 284, 649, 524, 719, 471, 584, 35, 50, 55, 770, 680, 464,
   5, 269, 580], dtype=object)

CodePudding user response:

I think this is what you're looking for :

import time
from datetime import date, datetime

time = time.time()
dt_obj = datetime.fromtimestamp(time)

print(time) # time in timestamp
print(dt_obj) #2021-10-07 11:38:14.023729
print(dt_obj.minute) #38

CodePudding user response:

In previos answer you told me that the time always show 1901-02-23 00:00:00

here is how to get time by sending a request:

from datetime import datetime
import requests

raw_req = requests.get('http://worldclockapi.com/api/json/cet/now')
date_time_elements = raw_req.json()

current_date = date_time_elements['currentDateTime']
time_zone = date_time_elements['timeZoneName']
epoch_time = date_time_elements['currentFileTime']
clean_date = datetime.strptime(current_date, "%Y-%m-%dT%H:%M%z")

print(f'Current Date: {clean_date.date()}')   # 2021-10-07
print(f'Current Time: {clean_date.time()}')   # 11:21:00

print(f'Time Zone: {time_zone}')              # Central Europe Standard Time
print(f'Epoch Time: {epoch_time}')            # 132780792600931671
print(f'Timestamp: {clean_date.timestamp()}') # 1633598460.0

print(f'Current minute: {clean_date.time().minute}') # 21

Note that you can change the location by changing 'cet' in the url to The abbreviation for your area

Like:

  1. cet = Central Europe Standard Time
  2. est = Eastern Standard Time
  3. utc = Coordinated Universal Time
  • Related