how to convert UTC to IST(indian standard time)

suppose i have a column of UTC given below i want to convert into IST so i want to add +5:30 hrs to my UTC column,so how could i convert.
08:29:54
08:30:55
08:31:56
08:32:57
08:33:58
08:34:59
09:00:00
09:01:01

 Accepted Answer

If you have R2014b or later, datetime will do this easily.
Try this:
TUCT = {'08:29:54'
'08:30:55'
'08:31:56'
'08:32:57'
'08:33:58'
'08:34:59'
'09:00:00'
'09:01:01'};
T = datetime(TUCT, 'InputFormat','HH:mm:ss', 'TimeZone','UCT')
T.TimeZone = 'Asia/Kolkata'
producing:
T =
8×1 datetime array
06-Feb-2020 08:29:54
06-Feb-2020 08:30:55
06-Feb-2020 08:31:56
06-Feb-2020 08:32:57
06-Feb-2020 08:33:58
06-Feb-2020 08:34:59
06-Feb-2020 09:00:00
06-Feb-2020 09:01:01
T =
8×1 datetime array
06-Feb-2020 13:59:54
06-Feb-2020 14:00:55
06-Feb-2020 14:01:56
06-Feb-2020 14:02:57
06-Feb-2020 14:03:58
06-Feb-2020 14:04:59
06-Feb-2020 14:30:00
06-Feb-2020 14:31:01

11 Comments

the output i am getting but i want to separate the date and time in two separate column
Try this:
TUCT = {'08:29:54'
'08:30:55'
'08:31:56'
'08:32:57'
'08:33:58'
'08:34:59'
'09:00:00'
'09:01:01'};
T = datetime(TUCT, 'InputFormat','HH:mm:ss', 'TimeZone','UCT')
T.TimeZone = 'Asia/Kolkata'
Date = datetime(T,'Format','dd-MMM-yyyy');
Time = datetime(T,'Format','HH:mm:ss');
IndiaTime = table(Date, Time)
producing:
IndiaTime =
8×2 table
Date Time
___________ ________
19-Feb-2020 13:59:54
19-Feb-2020 14:00:55
19-Feb-2020 14:01:56
19-Feb-2020 14:02:57
19-Feb-2020 14:03:58
19-Feb-2020 14:04:59
19-Feb-2020 14:30:00
19-Feb-2020 14:31:01
The table is the easiest way to work with them. They do not concatenate in a matrix and retain the desired formatting.
If you want to be able to add them to recreate the original array, you could use dateshift to eliminate the time part from Date (shift it to the 'start' of the 'day') then subtract the shifted Date array from the original array to get a duration.
>> t = datetime('now');
>> M = dateshift(t, 'start', 'day');
>> since = t-M;
>> tt = table(t, M, since, 'VariableNames', {'rightNow', 'midnight', 'sinceMidnight'});
>> isequal(t, M+since)
ans =
logical
1
how can we do the reverse process,suppose if we have
13:59:54
14:00:55
14:01:56
14:02:57
14:03:58
14:04:59
14:30:00
14:31:01 and we want this hour minute and seconds in three different columns as given below
13 59 54
14 00 55
14 01 56
14 02 57
14 03 58
14 04 59
14 30 00
I am not certain what you want.
Choose ‘dm1’ or ‘dm2’:
tc = { '13:59:54'
'14:00:55'
'14:01:56'
'14:02:57'
'14:03:58'
'14:04:59'
'14:30:00'};
dt = datetime(tc, 'InputFormat','HH:mm:ss');
dm1 = [hour(dt) minute(dt) second(dt)]
dm2 = dt;
dm2.Format = 'HH mm ss'
producing:
dm1 =
13 59 54
14 0 55
14 1 56
14 2 57
14 3 58
14 4 59
14 30 0
dm2 =
7×1 datetime array
13 59 54
14 00 55
14 01 56
14 02 57
14 03 58
14 04 59
14 30 00
You could also use the hms function with three outputs to get the hour, minute, and second components.
[thehour, theminute, thesecond] = hms(dt)
This also works if you have a duration array containing elapsed time since the start of the day.
du = dt - dateshift(dt, 'start', 'day')
[thehour2, theminute2, thesecond2] = hms(du)
@Steven — Thank you! I had forgotten about that option.
@star strider if i have to get the microseconds value then how will be the format to get the microseconds value for eg : i have "08:29:54.0127 " in my data but as per your code i was just able to get as "08:29:54" i also want the remaining value ,
Use repeated S to indicate how many places you want for fractions of a second. For example ss.SSSS
i tried using this but its showing as error that it is unable to convert
As Walter Roberson mentioned:
TUCT = "08:29:54.0127";
T = datetime(TUCT, 'InputFormat','HH:mm:ss.SSSS', 'TimeZone','UCT', 'Format','HH:mm:ss.SSSS')
T.TimeZone = 'Asia/Kolkata'
produces:
T =
datetime
13:59:54.0127

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!