Read in timestamps from weird time format

조회 수: 4 (최근 30일)
Heidi Hirsh
Heidi Hirsh 2019년 2월 18일
편집: Stephen23 2019년 2월 19일
Hello! I am trying to read in data collected in CA. The timestamps are UTC times recorded as a string that includes characters I do not need. Can you help me figure out how to read in a column of timestamps that look likes this (see below). I want to end up with mtimes or even just separate the timestamps parts so that I can use datenum myself to reconnect the parts. Thank you! This is a column in a csv file if that makes a difference.
'2015-05-07T05:48:59Z'
'2015-05-07T05:58:59Z'
'2015-05-07T06:18:59Z'
'2015-05-07T06:28:58Z'
'2015-05-07T06:38:59Z'
'2015-05-07T06:48:59Z'
'2015-05-07T06:58:59Z'
'2015-05-07T07:08:58Z'
'2015-05-07T07:18:59Z'
'2015-05-07T07:28:59Z'
'2015-05-07T07:38:59Z'
'2015-05-07T07:48:59Z'
'2015-05-07T07:58:59Z'
'2015-05-07T08:08:58Z'
'2015-05-07T08:18:59Z'
'2015-05-07T08:28:59Z'
'2015-05-07T08:38:59Z'
'2015-05-07T08:48:59Z'
'2015-05-07T08:58:59Z'
'2015-05-07T09:08:58Z'
  댓글 수: 1
Stephen23
Stephen23 2019년 2월 19일
편집: Stephen23 2019년 2월 19일
That is not a "weird time format" at all, they appear to be quite normal ISO 8601 timestamps:
ISO 8601 date formats are preferred for any situtuation where it is required to write dates that will be universally understood (e.g. data collection, filenames, data exchange, internet protocols, documentation, published articles, etc). Their ability to be sorted into chronological order with a basic character-sort makes them much more convenient to work with than any other date formats.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 2월 18일
If the data is stored in a cell array of character vectors, S, then
datetime(S, 'InputFormat', 'uuuu-MM-dd''T''hh:mm:ss''Z''', 'TimeZone', 'UTC')
  댓글 수: 5
Heidi Hirsh
Heidi Hirsh 2019년 2월 18일
Thank you! I thought I had essentially done that. This is what I had:
m1data = readtable('M1_buoy/m1buoydata.csv');
timestring = m1data.date_time; %times are UTC
%need to reformat time from this: {'2015-05-07T06:28:58Z'}
time = datetime(timestring, 'InputFormat', 'uuuu-MM-dd''T''hh:mm:ss''Z''', 'TimeZone', 'UTC');
But I reran it using your code and now the timestamps look fine. Thanks so much!
Walter Roberson
Walter Roberson 2019년 2월 19일
I had accidentally used hh where I should have used HH. With datetime(), hh is for 12 hour times, and HH is needed for 24 hour times.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Holidays / Seasons에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by