Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have a timestamp with Nanoseconds format that I want convert this to datetime in but I get error
import datetime
example_timestamp= '1629617204525776950'
example_timestamp= int(example_timestamp)
timestamp_to_date_time = datetime.datetime.fromtimestamp(example_timestamp).strftime('%Y-%m-%d %H:%M:%S,%f')
You should be using = to assign values and not :
You can do like this.
import datetime
example_timestamp = int('1629617204525776950')
timestamp_to_date_time = datetime.datetime.fromtimestamp(example_timestamp/1000000000).strftime('%Y-%m-%d %H:%M:%S,%f')
print(timestamp_to_date_time)
2021-08-22 07:26:44,525777
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
string = "C:\\folder\\important\\week1.xlsx"
I need to extract the file name alone, "week1.xlsx" from this string.
But for some reason, it doesn't work.
You can use basename:
import os
str(os.path.basename("C:\\folder\\important\\week1.xlsx"))
=> 'week1.xlsx'
Try:
filename = string[string.rfind('\\')+1:]
Here's more info on rfind().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a date with format in 'yyyy-dd-mm'. How do I convert to 'yyyy-mm-dd' format, been bit of a struggle with strftime and strptime!
Assuming the date is '1919-01-12', I want to see it in the format '1919-12-01'.
from datetime import datetime
date = '1919-01-12'
formatted_date = datetime.strptime(date, '%Y-%d-%m').strftime('%Y-%m-%d')
datetime.strptime(date, '%Y-%d-%m') creates a datetime object from a string, .strftime('%Y-%m-%d') formats the datetime as a string accoring to the specified pattern
try this code
import datetime
print (datetime.datetime.strptime("1919-01-12", "%Y-%d-%m").strftime("%Y-%m-%d"))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
import datetime
now = datetime.datetime.now()
print("Current date and time:")
print(now.strftime("%T-%m-%d %H:%M:%S"))
the now object created is why we have to use two times the datetime function
You are not using "two times the datetime function". In datetime.datetime.now(), the function is now. The first datetime is the package, and the second datetime is sub-package. The objects are grouped hierarchically for sake of better organization, for example alongside datetime.datetime there is also datetime.date (compare datetime.datetime.now() with datetime.date.today()).
If you do not want to use both levels each time, then instead of:
import datetime
you could do:
from datetime import datetime
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to import experimental time data which is not really a time, date, or datetime according to the python formalism. It is a time-elapsed format as Dd HH:MM:SS.m where the first time point would be 0d 00:00:00.00 and a point 26 hours later would be 1d 02:00:00.00.
Is there a way to use the datetime module to extract the appropriate time information without hard-coding a string search for the day and adding a multiple of 24 to the hour counter?
To convert your 'odd' format to datetime.timedelta object try:
from datetime import timedelta
import re
input = '1d 02:00:00.00'
def to_timedelta(input):
reg = re.search('([0-9])+d ([0-9]){2}:([0-9]){2}:([0-9]){2}\.([0-9]){2}', input)
ints = tuple(int(t) for t in reg.groups())
return timedelta(days=ints[0],
hours=ints[1],
minutes=ints[2],
seconds=ints[3],
milliseconds=ints[4])
td = to_timedelta(input)
print(td)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given timecode in this format
00:00:00,0
What is the best way to convert this into seconds.fractions_of_a_second?
Here's a link for some python code to handle SMPTE timecodes http://code.google.com/p/pytimecode/
Hope it helps...
The datetime module is your friend in this case
import datetime
time_string = "17:48:12,98"
t = datetime.datetime.strptime(time_string, "%H:%M:%S,%f")
seconds = 60 * t.minute * t.hour
print (seconds, t.microsecond)