How to shift date-time variable to a common start-date/time

5 ビュー (過去 30 日間)
David O'Brien
David O'Brien 2015 年 6 月 19 日
編集済み: Peter Perkins 2015 年 6 月 23 日
I have some data formatted as follows:
% Simple example as the "actual_dates" is a large char array.
common_date = datetime(14,1,1,0,0,0) % Define a common start date (01-Jan-0014 00:00:00)
actual_dates = '29-Jan-2014 13:57:30' % This is "char"
Then I run this:
t_diff = actual_dates - common_date % The shift value
and get this weird result:
t_diff = 17532325:57:35
The end goal is to have this:
shifted_date = actual_dates - t_diff
For this particular example, want to have shifted_date = '01-Jan-0014 00:00:00'
How can I fix this? I'm not sure how to convert the 'char' array into a datetime format.

採用された回答

Star Strider
Star Strider 2015 年 6 月 19 日
I don’t understand what you want to do, but converting the string to a datetime object is srtaightforward. Doing calculations on them is a bit more involved, since it requires the appropriate functions.
Try this to start:
common_date = datetime(14,1,1,0,0,0);
actual_dates = '29-Jan-2014 13:57:30';
act_date = datetime(actual_dates);
t_diff = between(common_date,act_date)
produces:
t_diff =
2000y 28d 13h 57m 30s
  2 件のコメント
David O'Brien
David O'Brien 2015 年 6 月 19 日
編集済み: David O'Brien 2015 年 6 月 19 日
All I needed to do was to shift multiple sets of recorded dates/times to all start at a certain arbitrary time/date.
Great. Thanks for your help! The between function and using datetime were just what I needed.
Star Strider
Star Strider 2015 年 6 月 19 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (2 件)

Katalin
Katalin 2015 年 6 月 19 日
Try this: shifted_date = datetime(actual_dates) - t_diff

Peter Perkins
Peter Perkins 2015 年 6 月 23 日
編集済み: Peter Perkins 2015 年 6 月 23 日
Just for the record, this
>> common_date = datetime(14,1,1,0,0,0)
common_date =
01-Jan-0014 00:00:00
>> actual_dates = '29-Jan-2014 13:57:30'
actual_dates =
29-Jan-2014 13:57:30
>> t_diff = actual_dates - common_date
t_diff =
17532325:57:30
does automatically convert the string (no need to explicitly call datetime on it, although of course it doesn't hurt). It seems a little funny that you are creating a datetime in the year 14 (not 2014), but I'll assume you meant to do that. The result simply says that there are 17.5 million hours between those two dates. This
>> t_diff.Format = 'y'
t_diff =
2000.1 yrs
is probably a more useful format, or, as StarStrider suggested, use the between function to get the difference in calendar units.

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by