# Understanding datetime compression types

Jan 16 · 4 min read

All datetime algorithms of the PoliDataCompressor use the same numerical compression method, called “NCOMP”.
This method seeks to fit the numeric sequences into the addresses of the bytes; for example, if we have a sequence of two numeric digits “97” and we would like to save it in Blockchain, we pay for two bytes, but, with we use this algorithm, we would record only one byte represented by ASCII 097 or the text “a”. Using it, we would save 50% of the space needed.

HANDS-ON

To facilitate our learning, open polidatacompressor.com in Google Chrome and right-click choose the last menu option that should be “Inspect” or if you prefer use the shortcut CTRL + SHIFT + I.
A window like the image below will appear.

If you are not in the “Console” option, navigate through the options until you find it.

Working with Dates (month, day and year).

Dates are different from numbers because we will never have a month greater than 12 and not a day greater than 31; so we can organize it in a way that becomes as few as possible for the greater accommodation of the bytes. So, if we had the date December 31, 2019, we could inform the month, the day and for last year. Thus, 12312019 would be the position of 12,312,019 which would be lower than position 20,191,231 (2019/12/31), lower than position 20,193,112 (2019/31/12).
To do this directly, use the NCOMP command on the return of the date_to_number1 function.

var d = new Date (“31 Dec 2019”); Ncomp (date_to_number1 (d));

Note that when compressing 8 digits “12312019” we has turned to 3 digits represented by bytes [211, 221, 187] (62%).

Working with dates (month and year)

If you need to store only month and year in Blockchain, you can use the following function, month_to_number1, combined with Ncomp.

var d = new Date (“Dec 2019”); Ncomp (month_to_number1 (d));

In this case, the reduction passed from 6 bytes “122019” to 3 bytes identified by the sequence [163, 220, 1], that is, a compression of 50%.

Working with dates and times

To compress dates with times, you can use two different types of functions, the first uses the Unix timestamp and can only be used if the date is greater than the year 1970 and the second, it orders the month, day, year, hours and minutes . These are datetime_to_number1 (p1) and datetime_to_number2 (p1).
See the compaction of the two below:

Using timestamp method
var d = new Date (“2019–12–31T23: 59”); Ncomp (datetime_to_number1 (d));
Using the second method
var d = new Date (“2019–12–31T23: 59”); Ncomp (datetime_to_number2 (d));

The first method transformed 10 chars in 4 (60%) and the second method transformed 12 in 5 (58.3%), however, it is better to store 4 characters in Blockchain than 5.

CAUTION: you must guarantee that dates will be greater than the year 1970.

Working with hours and minutes

If you have to enter hours and minutes, use the time_to_number1 (p1) function that will turn into time concatenated with minutes, and then use the Ncomp function to turn the content into bytes.
var d = new Date (“2019–12–31T23: 59”); Ncomp (time_to_number1 (d));

Note that the reduction was 50% because the original was 23:59 and was transformed into two bytes [55,9].

Working with hours, minutes and seconds:

For the situation where we have to register the seconds of the hour, the proposed algorithm is that everything be converted in seconds and then compressed.

var d = new Date (“2019–12–31T23: 59: 59”); Ncomp (time_to_number2 (d));

To decompress (all situations)

Following the last example, note that when we apply the Ndecomp command we have already returned the uncompressed value.

Ndecomp ([127, 81, 1])

How to deploy the library in your web application

To deploy just download the .ZIP file on the site itself, unzip and reference the files in the <HEAD> of your HTML page.

<script src=”PoliDataCompressor.js”></script>
<script src=”PoliDataCompressor-util.js”></script>

You must to reference the JQuery library too.