When export date/time to excel, change the time(mm:ss)..

3 ビュー (過去 30 日間)
Jeong_evolution
Jeong_evolution 2017 年 2 月 5 日
コメント済み: dpb 2022 年 10 月 12 日
Hi all.
I have a problem.
Now, time is 2017-02-05 21:35:32.
But I want to export 2017-02-05 21:00:00 except minute, scecond.
But when I use writetable(to .xlsx), time changed 2017-02-05 21:35:32 in excel.
t = datetime('now','TimeZone','Asia/Seoul','Format','yyyy-MM-dd HH:00:00') % 2017-02-05 21:00:00
T = table(t); % 2017-02-05 21:00:00
writetable(T,filename); % 2017-02-05 21:35:32
What is problem in this code?

採用された回答

Peter Perkins
Peter Perkins 2017 年 2 月 6 日
Use dateshift to round the datetimes down to the start of the hour before saving to the file.
  3 件のコメント
Peter Perkins
Peter Perkins 2017 年 2 月 7 日
I hear what you're saying. There's a tension between doc that is a giant reference manual and impenetrable for many, and doc that's completely examples and is more difficult to find specific details. We try to find a balance, but I hear what you're saying.
The page that I think you needed to find is this one. I'll pass your feedback on to the doc team. Thanks.
dpb
dpb 2017 年 2 月 7 日
編集済み: dpb 2017 年 2 月 8 日
Thanks, Peter, always appreciate hearing back. As a note I found that link by searching for dateshift from your answer. I did the backwards search because I was curious as to where the reference lived. I never thought (and really still don't quite think) that what I was looking for to answer OP's question as being related to Arithmetic and Plotting so never tried that link.
I also didn't recognize what was looking for from the short description under command window help datetime that does at least list (what I presume is comprehensive) the available methods/functions as it simply says
dateshift - Shift datetimes or generate sequences according to a calendar rule.
I guess after the fact I can see how rounding to hours could be construed to be covered in that description but certainly didn't ring the bell when I scanned it while crafting the Answer I gave OP. I was sure there had to be something better than the kludge I showed but definitely wasn't able to find it.
I do grok the TMW dilemma...being a fossil that started in mainframe days of the Philco 2000 with nuclear design application codes whose user manuals ran to hundreds of pages simply for building input decks and FORTRAN language manuals that were basically a minimal recasting of the Standard or vendor implementation of the day, I certainly do understand what precise documentation can run into. I also observed over time after the advent of the user terminal first, then WYSWIG editors and the evolution from "roll your own" FORTRAN to RAD environments like Matlab that the willingness and ability of new hires to read and comprehend the documentation that we old-timers took for granted became less and less something they could do without extensive remedial help in learning how to actually parse the descriptions. That tendency I think has continued to evolve towards the expectation that everything should be transparent--well, some things just aren't.

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

その他の回答 (1 件)

dpb
dpb 2017 年 2 月 5 日
編集済み: dpb 2017 年 2 月 5 日
What's wrong is the confusing nature of what the 'Format' property actually does with datetime variables; it only affects the output display of the time value, NOT the value of the time stored...example--
>> T=table(datetime('now','TimeZone','Asia/Seoul','Format','yyyy-MM-dd HH:00:00'));
>> t=T.Var1 % looks like an even hour, doesn't it?
t =
2017-02-05 23:00:00
>> whos t % is a datetime object, too...
Name Size Bytes Class Attributes
t 1x1 123 datetime
>> t.Format % the format is as noted to hours only
ans =
yyyy-MM-dd HH:00:00
>> t.Format='default' % now change that property
t =
05-Feb-2017 23:39:46
>>
and Voila! -- the actual time stored is revealed; the Format property hid what was really there.
You'll have to convert the time values you have to either datenum or datetime values by converting to the actual time values wanted. One way (not sure it's necessarily the most efficient) is to convert the output string with the desired format back to numeric internal form to get those values to be output.
Example--
>>t=datetime('now','Format','yyyy-MM-dd HH:00:00'); % a current time value
>> t.Format='default' % what is actual? (Does have MM:ss as expect)
t =
05-Feb-2017 09:02:48
>> t.Format='yyyy-MM-dd HH:00:00'; % set format to remove MM:ss
>> datetime(char(t)) % convert that string back to datetime
ans =
05-Feb-2017 09:00:00
>>
NB: Final result in default format show 00:00 for MM:ss; successfully rounded to hours only as desired.
>> t
t =
2017-02-05 09:00:00
>>
  2 件のコメント
Robert
Robert 2022 年 10 月 12 日
The question was to not export minutes and seconds.
Your answer exports 2017-02-05 09:00:00 while the question asks for 2017-02-05 09
How can that be achieved?
dpb
dpb 2022 年 10 月 12 日
As the note/text says, it was an example for the OP to mung upon as chose/needed/wanted to match need illustrating the way to get whatever format is wanted.

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by