Adding seconds to get new date

29 views (last 30 days)
Andrew Smelser
Andrew Smelser on 15 Apr 2019
Answered: Peter Perkins on 16 Apr 2019
I have some dates and then I have an ellapsed time for a transfer orbit (in seconds). I need to add the seconds (t.trans.trans12) to a date (DateString.launch) and want it to output the new date in the format 'yyyy-mm-dd HH:mm:ss'. I'm not really sure why its not working. I've tried everything I can find and isn't working quite how I want it.
So I need the new date (DateString.afterTrans12) so I can plug it into juliandate() so I can find the position of Mars and Earth at DateString.afterTrans12
If anybody has an easier way to do all of what I have below please share :)
%% Format Specifiers
epochValue = '2000-01-01';
formatIn = 'yyyy-MM-dd hh:mm:ss';
%% Transfer Time
t.trans.trans12 = seconds(2.236376148291764e+07); % seconds
%% DateStrings
DateString.epoch = datevec(datetime('2000-01-01 12:00:00','format',formatIn,'ConvertFrom','epochtime','Epoch',epochValue)); % J2000 epoch
DateString.start = datevec(datetime('2032-04-17 12:00:00','format',formatIn,'ConvertFrom','epochtime','Epoch',epochValue)); % mission start date
DateString.emergency = datevec(datetime('2039-11-03 12:00:00','format',formatIn,'ConvertFrom','epochtime','Epoch',epochValue)); % emergency date
DateString.launch = datevec(datetime('2040-04-17 12:00:00','format',formatIn,'ConvertFrom','epochtime','Epoch',epochValue)); % launch to planet date
DateString.afterTrans12 = []; % Need output in 'yyyy-mm-dd HH:mm:ss' for input of seconds;
%% Elapsed Time Data
T.launch = etime(DateString.launch,DateString.epoch); % between epoch and launch
T.emergency = etime(DateString.emergency,DateString.epoch); % between epoch and emergency
T.start = etime(DateString.start,DateString.epoch); % between epoch and start
%% Position Data
% ignore or erase this if you copy+paste to your computer and don't have the planet ephemeris package
position.earth.launch = planetEphemeris(juliandate(DateString.launch),'Sun','Earth');
position.earth.start = planetEphemeris(juliandate(DateString.start),'Sun','Earth');
position.earth.emergency = planetEphemeris(juliandate(DateString.emergency),'Sun','Earth');
position.mars.launch = planetEphemeris(juliandate(DateString.launch),'Sun','Mars');
position.mars.start = planetEphemeris(juliandate(DateString.start),'Sun','Mars');
position.mars.emergency = planetEphemeris(juliandate(DateString.emergency),'Sun','Mars');

Answers (1)

Peter Perkins
Peter Perkins on 16 Apr 2019
Andrew, it's not clear to me why you are converting datetimes to datevecs and then using etime. Not sure I've followed what you want to do, but this seems much simpler:
>> epoch = datetime('2000-01-01 12:00:00')
epoch =
datetime
01-Jan-2000 12:00:00
>> launch = datetime('2040-04-17 12:00:00')
launch =
datetime
17-Apr-2040 12:00:00
>> Tlaunch = launch - epoch % as hh:mm:ss
Tlaunch =
duration
353208:00:00
>> Tlaunch.Format = 'd' % as 24hr days
Tlaunch =
duration
14717 days
Here's how you add the seconds
>> afterTrans12 = seconds(123456)
afterTrans12 =
duration
123456 sec
>> launch = launch + afterTrans12
launch =
datetime
18-Apr-2040 22:17:36
>> JDLaunch = juliandate(launch)
JDLaunch =
2466263.42888889
>> %position.earth.launch = planetEphemeris(JDLaunch,'Sun','Earth');

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!