In this article I would like to discuss about DateTime type encoding and decoding by using ASN.1 DER encoding rules. I have already posted similar explanations for other data types: ASN.1

As any other data type DateTime is encoded by using the following structure:

  • Tag value. For DateTime must be 0x17
  • Data length. For DateTime must be 0x0d (13).
  • Actual encoded data.

RFC 5280 defines two DateTime standards: UTC for dates prior to 2050 year and Generalized Time after 2050 year. Though here I'll discuss about UTC time.

For example, take current date and time: 08 November 2010, 18:10:47. Each date and time part is encoded by using two digits with the following structure: yyMMddHHmmssZ. The last "Z" (capital) sign means invariant zone (UTC or GMT 0) and MUST be placed at the end of the string. Using above mentioned structure we can encode current time as: 101108181047Z. And now get each character corresponding ASCII position:

49 48 49 49 48 56 49 56 49 48 52 55 90. And convert them to hex:

0x31, 0x30, 0x31, 0x31, 0x30, 0x38, 0x31, 0x38, 0x31, 0x30, 0x34, 0x37, 0x5a.

And add explicit tag and length byte before encoded data:

17 0d 31 30 31 31 30 38 31 38 31 30 34 37 5a.

You're waiting something else? Don't wait, this is all what I wanted to show you. DateTime encoding is pretty simple!

Share this article:


Shourob Datta

Thank you for your nice tutorial. 

How do you decode it, 


301e 170d 3130 3130 3037 3030 3030 3030
5a17 0d31 3131 3031 3732 3335 3935 395a

Post your comment:

Please, solve this little equation and enter result below. Captcha