KDB: How to Convert DateTime to Formatted String? - string

I want to create a function in KDB that can convert a datetime object to a string based on the user inputted format of the string. Is there a way to do this in KDB?
In Python, it would be something like this:
format = "%m-%d-%Y_%H%M%S"
def f(format, dt):
return dt.strftime(format)

The library datetimeQ has some functionality for doing this as it isn't inbuilt into kdb.
Example functions included are:
q).dtf.format["yy-mm-dd hh:uu:ss.000"; 2018.06.08T01:02:03.456]
"18-06-08 21:02:03.456"
q).dtf.format["d mmmm, dddd ,yyyy"; 2018.06.18];
"18 June, Tuesday ,2018"
q).dtf.format["d/m/yyyy"; 2018.06.08]
"8/6/2018"

Now, qdate is available for datetime parsing and string conversation

Related

Dynamically formatting dates and times fetched from an API in iso format

I am fetching data from an API that comes in iso formated strings, for ex - example 2022-07-27T00:00:00.0000000+01:00
end_date=item['RunDate']
start_time=portion['StartTimeWTT']
I am trying to format the date to look like: yyyy-mm-dd and time hh:mm:ss
I have tried different solutions but none of them works
end_date1=item['RunDate']
end_date=datetime.date().strftime(end_date1,'%Y-%M-%d')
or datetime.fromisoformat
I would be grateful for any suggestions.
For me, the easiest way to handle date strings is by using the dateutil library. For example in your cited case:
from dateutil import parser
from dateutil.utils import default_tzinfo
from dateutil.tz import tzoffset
tx = '2022-07-27T00:00:00.0000000+01:00'
tz_data = tzoffset("EST", 0)
print(default_tzinfo(parser.parse(tx) , tz_data))
yields
2022-07-27 00:00:00+01:00

Convert number to datetime format

How do I Convert "1561994754" number to "2019-07-01T15:25:54.000000"
I have used :
import datetime
datetime.datetime.fromtimestamp(x['date'] / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
But I am getting 1970-01-18 19:53:14.754000, can you please guide me to correct function?
Thanks,
Aditya
Removing the / 1000 gives me '2019-07-01 08:25:54.000000', It seems like there was a unit mismatch in your expression. To exactly match the format you're asking for, datetime.datetime.fromtimestamp(x['date'], tz=datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f') produces '2019-07-01T15:25:54.000000 (leaving the timezone argument blank defaults to using local time, but the human-readable date in your question uses UTC)
You can try like this!
String myString = String.valueOf(1561994754);
DateFormat format = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
Date date = format.parse(myString);

Convert a custom formatted date from string to a datetime object

I am trying to convert a string to a datetime format which has the following format
YYYYMMDD:HHMM
As an example:
20200712:1834 which is 2020/07/12 18:34
It is not difficult to extract the information from the string one by one and get year, month, day and the time. But I was wondering if there is a suphisticated way of doing this. For example, I tried the following:
from datetime import datetime
date_time_str = '20200712:1834'
date_time_obj = datetime.strptime(date_time_str, '%y%m%d:%H%M')
Could someone kindly let me know?

Change how datetime object is printed from print()?

When printing dates in the datetime module of Python 3.6, they look like:
>> from datetime.date import date
>> print(date(2018,1,30))
2018-01-30
Is it possible to change the format of the output of print() for datetime.date objects?
My desired outcome is:
>> print(date(2018,1,30))
30/01/18
You can use datetime formatting function - strftime - taking a formatting string described in detail here.
You may also want to review Advanced date formatting from the following answer: How to print a date in a regular format?

Python string to datetime-date

I've got lots of dates that look like this: 16.8.18 (American: 8/16/18) of type string. Now, I need to check if a date is in the past or future but, datetime doesn't support the German format.
How can I accomplish this?
from datetime import datetime
s = "16.8.18"
d = datetime.strptime(s, "%d.%m.%y")
if d > datetime.now():
print('Date is in the future.')
else:
print('Date is in the past.')
Prints (today is 20.7.2018):
Date is in the future.
The format used in strptime() is explained in the manual pages.

Resources