3

I'm a beginner to KDB/Q Langage, and I'm looking to find a way to convert a date object (or its string representation) to the format MM/DD. For example, I have 2016-09-23 and I would like to format it to 09/23.

There is another thread relating to this subject, but I am not sure how helpful it'd be : How to produce a formatted date string in Q/KDB?

In Q Language, is there any function similar to the ones like convert(varchar, getdate(), 112) in SQL?

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Edward Cai
  • 33
  • 3

5 Answers5

4

No such convert function exists in Kdb+ - but you convert it yourself with some basic string manipulation in Kdb - an example:

q) show dt:.z.d
    2016.09.26
q)"/" sv 1 _ "." vs string dt
    "09/26"

See vs(vector from scalar) and sv (scalar from vector) for more information on splitting / joining a string.

The code above also uses _(drop) to drop the first portion (year) of the date after the sting has been split by "."

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
MdSalih
  • 1,978
  • 10
  • 16
4

If your strings are in a standard format (i.e YYYY-MM-DD), it will be quicker to use indexing @ depth.

q)list:("2016-09-23";"2016-10-23") 
q)5_'.[list;(::;4 7);:;"/"]
"09/23" 
"10/23"
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Connor Gervin
  • 935
  • 5
  • 16
1

An alternative:

q)"/"0:(" **";"-")0:enlist "2016-09-23"
"09/23"
Sean O'Hagan
  • 1,681
  • 8
  • 14
0

For a very large vector, and provided you're not set on using a "/", this will be pretty fast:

q)dts:.z.d+til 1000000;
q)string[dts][;8 9 4 5 6]
"29.06"
"30.06"
"01.07"
"02.07"

If you want the "/":

"/"^string[dts][;8 9 10 5 6]

Misread the question; this is for DD/MM format.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
emc211
  • 1,369
  • 7
  • 14
0

Check out this GitHub library for datetime formatting. It supports the excel way of formatting date and time. Though it might not be the right fit for formatting a very large number of objects.

q).dtf.format["mm/dd"; 2016.09.23]  
"09/23"

q).dtf.format["yy/m/d"; 2016.09.03]  // another example
"16/9/3"
nyi
  • 3,123
  • 4
  • 22
  • 45