convert current date and time to char

조회 수: 9 (최근 30일)
Dion Theunissen
Dion Theunissen 2022년 5월 4일
댓글: Stephen23 2022년 5월 6일
Hi,
I need 2 parameters. The first is the current date and time in the format: '2022-05-02T08:00:00Z'
The second is that datetime minus 6 hours as a char.
Anyone who can help me?
endtime = '2022-05-02T13:59:00Z'; %this needs to be the current datetime - 6 hours
starttime = '2022-05-02T08:00:00Z'; %This needs to be the current datetime

채택된 답변

Rik
Rik 2022년 5월 4일
편집: Rik 2022년 5월 4일
datestr(now-6/24,31)
ans = '2022-05-04 07:01:11'
datestr(now_UTC-6/24,31)
ans = '2022-05-04 07:01:11'
If you want to force the use of UTC+0 as the timezone (as your Z seems to suggest), you might be interested in getUTC.
In the case of the online run tool there is no difference, as most servers (in my experience) already use UTC+0 as their time zone.
function t=now_UTC
persistent timezone_delta
if isempty(timezone_delta),timezone_delta=getUTC-now;end
t=now+timezone_delta;
end

추가 답변 (2개)

Stephen23
Stephen23 2022년 5월 4일
Avoid deprecated DATESTR and DATENUM.
one = datetime('now','Format','yyyy-MM-dd''T''HH:mm:ss''Z''')
one = datetime
2022-05-04T12:11:00Z
two = one - hours(6)
two = datetime
2022-05-04T06:11:00Z
  댓글 수: 2
Dion Theunissen
Dion Theunissen 2022년 5월 6일
Is this also possible to distract 1 month?
Stephen23
Stephen23 2022년 5월 6일
"Is this also possible to distract 1 month?"
two = one - calmonths(1)

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


John D'Errico
John D'Errico 2022년 5월 4일
편집: John D'Errico 2022년 5월 4일
Easier than you think.
First, what is the current time?
format long g
CurrentTime = now
CurrentTime =
738645.50979622
That might not seem terribly helpful. However, datestr will help a lot.
datestr(CurrentTime)
ans = '04-May-2022 12:14:06'
You want to see an offset of 6 hours before that time. That is 1/4 of a day.
datestr(CurrentTime - 1/4)
ans = '04-May-2022 06:14:06'
So we are getting there. But of course, it is not exactly in the format you wish to see. First we will fix the date part, and you want that in the form of Year, Month, Day. The integer part of the time reported by now is the date. For example, we can do this:
datestr(fix(CurrentTime),'yyyy-mm-dd')
ans = '2022-05-04'
And that gives you the date in a format as you desire. You can format the time part similarly.
datestr(CurrentTime - fix(CurrentTime),'HH:MM:SS')
ans = '12:14:06'
And then you wanted a spare T and Z in there for some reason. So we might write a little helper function like this:
mytimeformat = @(CT) [datestr(CT,'yyyy-mm-dd'),'T',datestr(CT - fix(CT),'HH:MM:SS'),'Z'];
mytimeformat(CurrentTime)
ans = '2022-05-04T12:14:06Z'
As you can see, that formats a time and date in the way you want it, specifically with the extra characters in there. The second time you wanted to see was 6 hours before that.
mytimeformat(CurrentTime - 1/4)
ans = '2022-05-04T06:14:06Z'
I'm sure there re other ways to do this, but the point is, when you have a difficult problem to solve in MATLAB, start from something you can find. Then work with it. Perhaps you can massage that result into something close to what you need. Then keep on working at it. In the end, you will see it was easier than you thought all along.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by