Date iso format python - python

I have this date form in python that I am writing on an api: 2022-06-01T10:36:56.000Z How can I turn this into a second? Thanks for your comments
I would like to use the seconds I have to do addition division and then go back to day/date/hour format
I can't find any content on the internet

Python cannot handle all type of iso formatted date strings. So you can use isodate library or remove .000Z from the string parsing.
You can be more clear on seconds though, is it total seconds elapsed from 1970, Jan 1 or seconds part in the date ?
k = datetime.fromisoformat("2022-06-01T10:36:56.000Z".replace(".000Z", ""))
k.timestamp()

Related

datetime string (from python) to matlab

I have a datatime string (which comes from django/python) which looks like so:
datatime_str='2020-08-18 16:48:13.722422+00:00'
I then do, in Matlab 2018a:
fmt_dt='yyyy-MM-dd HH:mm:ss.SSSSSS+HH:mm';
datetime(datatime_str,'TimeZone','local','Format',fmt_dt);
and I get:
2020-08-18 00:00:13.722422+00:00
I am not sure what it is that I am doing wrong, but the result is obviously wrong :(
Any help would be great
Yes the +00:00 should be formatted as timezone, not hours, minute. However, you can set the display format as you would like, and this could be different from the input. The default Matlab datetime display format discards the fractional seconds (and also the timezone I think). For example:
fmt_dt_input='yyyy-MM-dd HH:mm:ss.SSSSSSxxxxx';
fmt_dt_show='yyyy-MM-dd HH:mm:ss.SSSSSS xxxxx';
datatime_str='2020-08-18 16:48:13.722422+00:00';
t = datetime(datatime_str,'InputFormat',fmt_dt_input,'TimeZone','local','Format',fmt_dt_show)
Has as output: 2020-08-18 16:48:13.722422 +00:00
EDIT: btw this info on datetime can be found here
your input string contains a UTC offset at the end, +00:00 which you parse as hours and minutes - that is why they are set to 00:00 in the result. Use e.g.
datetime('2020-08-18 16:48:13.722422+00:00', 'InputFormat', 'yyyy-MM-dd HH:mm:ss.SSSSSSZ', 'TimeZone', 'UTC')
instead (change the TimeZone parameter to whatever you need).

Python Time without seconds

So I need to display current time, I only print now.hour and now.minutes, however there is always 00 that's displayed for seconds that I would like to take away
this is my code
now = datetime.datetime.now()
print(datetime.time(now.hour, now.minute))
this is my out put
16:40:00
You can use strftime to format the time stored in now.
now
>2020-02-17 09:47:52.429173
print(datetime.strftime(now, '%H:%M'))
09:47
The string located in datetime.strftime(--, 'here') defines the formatting that will be returned. You can find more of strftime's formatting specifications here.

Converting "2013-01-06T22:25:08.733" to "2013-01-06 22:25:08" in python

I have a big .csv file which holds machine log data. One of the fields is timestamp. It stores date and time as shown in the title and I would like to drop the milli seconds and convert it into the format also shown in title. Can anyone help me with that? Im new to Python and Ipython.
Much obliged.
For your special case, this should suffice:
t.replace('T', ' ')[:19]
But I would recommend, that you use the datetime module of the standard library instead, so your time conversion also could be internationalized.
You can use easy_date to make it easy:
import date_converter
new_date = date_converter.string_to_string("2013-01-06T22:25:08.733", "%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S")

How do I get the current time and date of the operating system

How do I get the current time and date of the operating system (the one in the clock). I tried to use datetime.now(). But it returns different value.
As suggested by mcalex I've rechecked the time and date setting and this has always been like this:
Use time.localtime().
From https://docs.python.org/2/library/time.html#time.localtime
Like gmtime() but converts to local time. If secs is not provided or
None, the current time as returned by time() is used. The dst flag is
set to 1 when DST applies to the given time.
You can use the Python time module for various time-related functions. It appears you are requesting the following format:
Day Month Hour:Min:Sec Year
If so, you can use the following:
>>> import time
>>> time.asctime(time.localtime())
'Mon Jun 30 22:19:34 2014'
To convert to a specific format, you can use the following function:
time.strftime(format[, t])
This converts a struct_time object tm representing a time as returned by gmtime() or localtime() to a string. See the following link for more info on the format codes:
https://docs.python.org/2/library/time.html#time.localtime
Source: Beazley, D. M., "Python Essential Reference", 4th. Ed.

validate date : python

I want to know how to convert different format dates to expected format in python .
ex : i want to get this format : 2/29/2012
['2012-02-01 // 2012-02-28', '2/15/2012', '2/13/2012', '2/14/2012', '2/23/2012', '2/18/2012', '2/29/2012']
How to check today date in the range '2012-02-01 // 2012-02-28'
Share your suggestions
Use the datetime library in python. You can just compare two different datetime.datetime objects. And you can separate the year, month, date thing to put it in anyform you want.
Check this link for all the library details.
http://docs.python.org/library/datetime.html
Hope that helped.
The dateutil python library parses a wider variety of date formats than the standard datetime module.

Categories