How do I get MATLAB to recognize a BCE

1 回表示 (過去 30 日間)
CHRISTY
CHRISTY 2024 年 2 月 16 日
コメント済み: CHRISTY 2024 年 2 月 16 日
Hi all, I am trying to make a time calculator and want the user to be able to input negative numbers. I have so far converted this negative into a positive number, and just need to add BCE onto the end and have it as a valid input to the time difference calculator. How can I get Matlab to accept BCE attached to the end of the year in the MM/DD/YYYY format as a valid year? What is the input formula that I have to put in and how would I tell it to do so?

回答 (1 件)

Steven Lord
Steven Lord 2024 年 2 月 16 日
Rather than trying to create a string containing a negative year to represent a BCE date and passing that string into datetime, I'd call datetime with three inputs containing the year, month, and day data. In the example below I'm also specifying the format with which to display the datetime, using G to display the era rather than displaying the year as a negative number.
y = -38;
m = 4;
d = 15;
April15BCE = datetime(y, m, d, Format="dd MMM y G")
April15BCE = datetime
15 Apr 39 BCE
A year value of 0 corresponds to 1 BCE, so April15BCE lists a year of 39 BCE for y = -38.
datetime(0, 4, 15, Format="dd MMM y G")
ans = datetime
15 Apr 1 BCE
datetime(1, 4, 15, Format="dd MMM y G")
ans = datetime
15 Apr 1 CE
  1 件のコメント
CHRISTY
CHRISTY 2024 年 2 月 16 日
Thank you, my boss (learning how to code for work) wants the input format to be MM/DD/YYYY and enable a user to enter a negative number for year and have it calculate the duration. Is there a way to allow a negative year using this format technique you suggested?

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

カテゴリ

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