Search code examples
stringdatetimeformatkdb

KDB: How to Convert DateTime to Formatted 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)

Solution

  • 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"