Borrar filtros
Borrar filtros

Adding to existing date value

9 visualizaciones (últimos 30 días)
Harsh Trivedi
Harsh Trivedi el 20 de Abr. de 2023
Editada: Stephen23 el 20 de Abr. de 2023
Hello Everyone.
I have a 1x1 datetime matrix (variable nam: date_for_entry) containing vaue 03/27/2023. I want to add 5 days to this and get the updated date value.
Using caldays does not update the month. i.e. date_for_exit = date_for_entry + caldays(5) gives me value of 03/01/2023 instead of 04/01/2023.
I tried using addtodate. This gave me correct value, however, converting back to datetime format gave me 03/01/2023. Can someone please help?
Thanks!

Respuestas (2)

Stephen23
Stephen23 el 20 de Abr. de 2023
Editada: Stephen23 el 20 de Abr. de 2023
"Using caldays does not update the month"
Yes, it does:
dt = datetime(2023,3,27, 'Format','u-MM-dd')
dt = datetime
2023-03-27
dt = dt+caldays(5)
dt = datetime
2023-04-01
Apparently you did something else, but because you did not upload any data or show any code, we cannot debug what you did. Debugging invisible code that runs on non-existent data is challenging. It is much easier when you actually give us enough information to help you.
EDIT: the problem is very easy to replicate, we just need to mix up the months and minutes:
dt = datetime(2023,5,27,1,3,2, 'Format','mm/dd/u') % MINUTES/dayofmonth/year
Warning: The format 'mm/dd/u' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
dt = datetime
03/27/2023
dt + caldays(5) % these are still MINUTES/dayofmonth/year
ans = datetime
03/01/2023
@Harsh Trivedi: the problem is very simple: you have imported or defined the DATETIME object incorrectly, most likely by swapping the month and minutes. Solution: fix your DATETIME object, using the FORMATs give here:
Deprecated ADDTODATE cannot fix this. The solution is to fix the cause of the problem.
  2 comentarios
Steven Lord
Steven Lord el 20 de Abr. de 2023
The problem might be in the Format of the datetime.
dt = datetime(2023, 3, 27, 12, 3, 45, 'Format', 'u-mm-dd')
Warning: The format 'u-mm-dd' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
dt = datetime
2023-03-27
dt2 = dt + calmonths(1)
dt2 = datetime
2023-03-27
That addition didn't change the minute data (which is what was used in the Format, as the warning indicates) but it did change the month data as you can see by converting the datetime into its component parts using datevec.
datevec(dt)
ans = 1×6
2023 3 27 12 3 45
datevec(dt2)
ans = 1×6
2023 4 27 12 3 45
If you suppressed the warning that datetime usually issues, using the minute identifier instead of the month identifier (or vice versa) would be easier to overlook.
Stephen23
Stephen23 el 20 de Abr. de 2023
Editada: Stephen23 el 20 de Abr. de 2023
@Steven Lord: yes, I was also wondering about that. After a few seconds experimentation, I came up with an example that demonstrates exactly what the OP observes.
dt = datetime(2023,5,27,1,3,2, 'Format','mm/dd/u')
Warning: The format 'mm/dd/u' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in datetime formats.
dt = datetime
03/27/2023
dt + caldays(5)
ans = datetime
03/01/2023

Iniciar sesión para comentar.


VBBV
VBBV el 20 de Abr. de 2023
Editada: VBBV el 20 de Abr. de 2023
d = datetime(2023,3,27, 'Format','u-MM-dd')
d = datetime
2023-03-27
d = datetime(addtodate(datenum(d),5,"day"),'ConvertFrom','datenum','Format','u-MM-dd')
d = datetime
2023-04-01
If you have tried using addtodate you need to convert to datenum first , then use datetime and convert back to datetime in desired format
  1 comentario
VBBV
VBBV el 20 de Abr. de 2023
May be you have used addtodate incorrectly WITHOUT specifying the number of days as argument in function

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by