Convert Timestamp to Integer

How can I convert the following timestamp into the an integer form, ideally milliseconds after 1970....

       s = '2014-02-11-00_40_05'

I have tried using:

       out = datevec(s)

However I receive an error saying 'Too many date fields in 2014-02-11-00_40_0'

Thanks

1

2 Answers

Try datenum with a format specifier:

>> datenum(s,'yyyy-mm-dd-hh_MM_ss')

ans =

   7.3564e+05

Convert to epoch:

mtime = datenum(s,'yyyy-mm-dd-hh_MM_ss');
unix_time = round(8.64e7 * (mtime - datenum('1970', 'yyyy')))

Assuming 02 in your example is month (otherwise change format string in the obvious way):

datenum('2014-02-11-00_40_05','yyyy-mm-dd-HH_MM_SS')

gives you seconds after Jan-1-0000. From that it's easy to get miliseconds after 1970:

( datenum('2014-02-11-00_40_05','yyyy-mm-dd-HH_MM_SS') - ...
  datenum('1970-01-01-00_00_00','yyyy-mm-dd-HH_MM_SS') ) * 1000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest