Convert datetime to numeric - preserve date format
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a timetable with dates (datetime data type) in the format 'yyyy-MM-dd HH:mm'. I want to convert these to a numeric data type (double) but keeping the exact numbers of each date such that the datetime '2023-02-27 14:00' is converted to the double '202302271400'. The function yyyymmdd() allows me to do this for the date, but not the hour and minute, and none of the other built in functions to convert to datenumber seem to have this option. Thanks for any insights to this problem.
採用された回答
Les Beckham
2023 年 2 月 27 日
編集済み: Les Beckham
2023 年 2 月 28 日
Edited to work with datetime array vs a single datetime.
d = datetime(['2023-02-27 14:00'; '2023-02-27 15:00']) % test data - replace with your timetable
d = 2×1 datetime array
27-Feb-2023 14:00:00
27-Feb-2023 15:00:00
s = string(d, 'yyyyMMddHHmm')
s = 2×1 string array
"202302271400"
"202302271500"
format long
% f = cellfun(@(s)sscanf(s, '%f'), s) % original method of converting to double
f = double(s) % An easier/cleaner method. Thanks to Walter for reminding me about this.
f = 2×1
1.0e+11 *
2.023022714000000
2.023022715000000
compose('%.0f', f)
ans = 2×1 cell array
{'202302271400'}
{'202302271500'}
10 件のコメント
Just use the 'Format' for this, then convert it to a string or char array in either the same line or a separate assignment —
d = datetime(['2023-02-27 14:00'; '2023-02-28 15:00'], 'Format','yyyyMMddHHmm')
d = 2×1 datetime array
202302271400
202302281500
ds = string(d)
ds = 2×1 string array
"202302271400"
"202302281500"
dc = char(d)
dc = 2×12 char array
'202302271400'
'202302281500'
.
Les Beckham
2023 年 2 月 27 日
I thought about that approach, but OP says that they already have the timetable. Who knows what format it was created with. Besides, they (for some unknown reason) wanted the result to be a float. Hence the sscanf call. I'll edit my answer to make it clear that the first line represents test data that will be replaced with the existing timetable.
Star Strider
2023 年 2 月 27 日
There are single quotes around the desired ‘double’ result, so there may be a bit of confusion on the OP’s part in that regard.
Les Beckham
2023 年 2 月 27 日
Good point. I saw that too, but they said "numeric type (double)".
Note that the compose command at the end was just to show that the values in f (the double result) are correct by printing them out -- since the exponential notation of the display of f makes it hard to tell if the decimal is in the right place.
Elin Jacobs
2023 年 2 月 27 日
Thank you both, this works as intended. Sorry about the confusion on my single quotes around the double, I'll be more careful next time. Really appreciate your help!
Walter Roberson
2023 年 2 月 28 日
You should not need sscanf of string array: just double() the string array
Les Beckham
2023 年 2 月 28 日
Of course, thanks for reminding me of that. I've edited the answer to reflect this approach.
Campion Loong
2023 年 3 月 29 日
@Walter Indeed. The question is meant for @Elin Jacobs. I mistook @Les Beckham to be the OP. Let me restate:
@Elin Although you can turn datetime into 'formatted numbers' with any of the above answers, would you mind elaborating on what makes you do that in the first place? Are you interfacing with another system that needs numerics representation of time in some way (like a database)?
Elin Jacobs
2023 年 3 月 29 日
@Campion Loong You are right, I'm interfacing with a (poorly designed) database for a local weather station.
Campion Loong
2023 年 3 月 29 日
@Elin Jacobs Thanks for confirming. That (unfortunately) makes sense.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
