# how to insert timestamp into kdb?

**URL:** <https://forum.kx.com/t/how-to-insert-timestamp-into-kdb/11827>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [October 24, 2018, 8:50am UTC](https://forum.kx.com/t/how-to-insert-timestamp-into-kdb/11827 "2018-10-24T08:50:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hzadonis](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@hzadonis](https://forum.kx.com/u/hzadonis)\
**Post date:** [October 24, 2018, 8:50am UTC](https://forum.kx.com/t/how-to-insert-timestamp-into-kdb/11827/1 "2018-10-24T08:50:00Z")

</div>

Hi, Masters:  
&nbsp; I have a date time value(format is YYYY.MM.DD) and a time value(mili\_second, which is kdb standard `time format).

&nbsp; According to the API of C to K, it was said “kz” and “ymd” functions should be applied while transfer timestamp data to KDB. My code for C++ are:

&nbsp; &nbsp; K temp\_kxdata;

&nbsp; &nbsp; temp\_kxdata = kz(ymd(YYYY, MM, DD) + mili\_second);

&nbsp; But RDB reports it is “Invalid DateTime”.

&nbsp; So, how to convert it into KDB’s timestamp?

Regards

Zheng

---

<div class="post-metadata">

**Author:** ![hzadonis](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@hzadonis](https://forum.kx.com/u/hzadonis)\
**Post date:** [October 24, 2018, 10:21am UTC](https://forum.kx.com/t/how-to-insert-timestamp-into-kdb/11827/2 "2018-10-24T10:21:00Z")

</div>

The example of mili\_second is 60545983  
in kdb, `time$60545983

output is:&nbsp;16:49:05.983

I need the datetime in kdb is: 2018.10.24T16:49:05.983

BTW, today is 1024, Happy Developer’s Day!

---

<div class="post-metadata">

**Author:** ![cormac\_ross](https://avatars.discourse-cdn.com/v4/letter/c/7ea924/32.png) [@cormac\_ross](https://forum.kx.com/u/cormac_ross)\
**Post date:** [October 24, 2018, 12:08pm UTC](https://forum.kx.com/t/how-to-insert-timestamp-into-kdb/11827/3 "2018-10-24T12:08:00Z")

</div>

Hi Zheng,

The underlying datatype of datetime is a float value representing days since kdb epoch (2000.01.01), so for example, if we do a cast, we can see:

q)“f”$2000.01.03T18:00:00.00

2.75

So, in order for you to make a datetime from the milliseconds as integer, you need to divide by the number of milliseconds in a day, i.e.

kz(ymd(YYYY, MM, DD) + mili\_second/86400000.0)

However, its worth pointing out that datetime as a datatype is deprecated, replaced by the timestamp type. This is the same size (8 bytes) but gives nanosecond precision. The underlying value for timestamp is long integer nanoseconds from the epoch date. To make one of these types in C, you could do the following:

ktj(-KP, (mili\_second\*1000000)+(ymd(YYYY,MM,DD)\*86400000000000))

Obviously this would require a schema change, so it doesnt solve your immediate problem, but it is worth noting for the future

Regards

Cormac

---

<div class="post-metadata">

**Author:** ![hzadonis](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@hzadonis](https://forum.kx.com/u/hzadonis)\
**Post date:** [October 25, 2018, 8:41am UTC](https://forum.kx.com/t/how-to-insert-timestamp-into-kdb/11827/4 "2018-10-25T08:41:00Z")

</div>

Hi, Cormac:  
&nbsp; Thank you so much!

&nbsp; Yes, the code works. As you said, it’s reasonable to treat the timestamp as a long integer number(nano second format), so convert both mili\_second and date into the nano seconds and plus them together matches KDB+ `timestamp format.

Thanks!

Zheng
