How to check whether datetime is within range

25 visualizaciones (últimos 30 días)
A.
A. el 8 de Abr. de 2022
Comentada: Star Strider el 8 de Abr. de 2022
This is the code:
>> C = strsplit('02.02.1957-13.02.1957','-')
C1=datetime(C{1},'InputFormat','dd.MM.yyyy')
C2=datetime(C{2},'InputFormat','dd.MM.yyyy')
C =
1×2 cell array
{'02.02.1957'} {'13.02.1957'}
C1 =
datetime
02-Feb-1957
C2 =
datetime
13-Feb-1957
Now, I would like to know, if e.g. '03.02.1957' is within '02.02.1957-13.02.1957'. Ist there a simple function for this? For 13.02.1957 the value should also be "true".
Thank you!

Respuesta aceptada

Star Strider
Star Strider el 8 de Abr. de 2022
There are several ways to do this, all using logical indexing. The easiest way is with the isbetween function.
C = strsplit('02.02.1957-13.02.1957','-');
C1=datetime(C{1},'InputFormat','dd.MM.yyyy');
C2=datetime(C{2},'InputFormat','dd.MM.yyyy');
Query = datetime('03.02.1957','InputFormat','dd.MM.yyyy');
TF = isbetween(Query, C1, C2)
TF = logical
1
So ‘Query’ is in the interval!
.
  2 comentarios
A.
A. el 8 de Abr. de 2022
Thank you very much!
Star Strider
Star Strider el 8 de Abr. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by