Clear Filters
Clear Filters

Converting date into date and hours

1 view (last 30 days)
zafar khan
zafar khan on 21 Sep 2017
Answered: Peter Perkins on 21 Sep 2017
I have half hourly energy data and need to convert the date into hourly format. At present the date is dd/mm/yyyy and hour is 0.5,1.0,1.5,... I want to get it as yyyy-mm-dd HH:MM:SS Any idea how to do it??
  1 Comment
James Tursa
James Tursa on 21 Sep 2017
Please be more specific. What is the exact format of your input data (the date and hour data)? Character string array and double array? Cell arrays? Or ...? And what do you want for the result ... a cell array of character strings? Or ...?

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 21 Sep 2017
Use datetimes and durations:
>> t0 = datetime('today','Format','yyyy-MM-dd HH:mm:ss')
t0 =
datetime
2017-09-21 00:00:00
>> t = t0 + hours(0:.5:3)'
t =
7×1 datetime array
2017-09-21 00:00:00
2017-09-21 00:30:00
2017-09-21 01:00:00
2017-09-21 01:30:00
2017-09-21 02:00:00
2017-09-21 02:30:00
2017-09-21 03:00:00

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!