Validate the current variable value

How to verify of the current variables has date in a boolean way like
i have str value 16.5.2021 this i got by regex from pdf text
but instead of date it can have some other values like days , weeks
so how i verify it has date , days , weeks ?

for example u may consider below text , i need to first find date if its not date then find day or weeks then calculate date in dd/MM/yyyy format

Today is 16.02.2021
Today is 02days past holiday
Today is 03 week of May

  1. To verify if it’s in dd.MM.yyyy format, follow this:
    DateTime.TryParseExact(Input,“dd.MM.yyyy”,System.Globalization.CultureInfo.CurrentCulture,System.Globalization.DateTimeStyles.None,Output)

You can use this command to verify if it’s a date. This will return true or false based on date validation. Here “Input” is a string which contains your date. Output is a datetime variable which stores the parsed date after this command is executed.

Below are the links to read more about TryParseExact method and CultureInfo:

  1. To check for the words “Days” or “weeks” , you can use .contains() method for your string variable

similarly can we get the Boolean value for regex , if we get the output True else False ?

Have a boolean variable eg isDate initiated as true

Use a try catch.

In the try, attempt to convert the value to date. I.e Convert.ToDateTime(inputString, “dd.MM.yyyy”)

In the catch section , catch System Exception and assign isDate to False

var1 = Convert.ToDateTime(inputString, “dd.MM.yyyy”) throws error for date 17.5.2021
seems like it works for the date in US date format
i wanted to first check if the variable is date then add days
it throws error , for var1 not datetime format
and inputString is not correct format

if its date then add days and convert into (“dd/MM/yyyy”) format

Use the below

Date.ParseExact(inputString,“dd.M.yyyy”, system.Globalization.CultureInfo.InvariantCulture)

Did you mean regex to check date? We can’t verify a date with regex. There are chances that even 32.13.2021 is also considered as date, if we use regex.

If you just want to check if your string contains one or more digits, then use:
System.Text.RegularExpressions.Regex.Match(StringVariable,"\d+").Success
This will return boolean

@Sakshi_Jain @Surya_Narayana_Korivipadu - datetime.tryparseexact will help here…Please check below…

Yes @prasath17 . I suggested the same in my post

1 Like

See I have shared screeshot of immediate window and datatimeparse doesnt work
please consider date as 17.05.2021
and add 2 days in this
so output should 19.05.2021

3.2.2021 still satisfies the US date format

@Sakshi_Jain - please show us what you tried in the immediate window?

I have a screenshot of add days in your other post …

Please check the output of these values above
Date1 = “16.2.2021”
Date1.ToString

(Convert.ToDateTime(Date1.ToString))
Datetime.ParseExact(Date1.ToString,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)
Datetime.ParseExact(Convert.ToDateTime(Date1),“dd.MM.yyyy”,System.Globalization.CultureInfo.InvariantCulture)
(Convert.ToDateTime(Date1.ToString)).AddDays(Cint(Regex.Split(Days,"-")(1))).ToShortDateString

Cint(Regex.Split(Days,"-")(1)) as int value

@Sakshi_Jain - Please understand the date formats…If your date is 17.05.2021 then it is dd.MM.yyyy …in the below code you have given dd/MM/yyyy that’s why it is not working…

Datetime.ParseExact(Date1.ToString,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

ConvettoDatetime

Date I am considering above is string datatype and value is Date1 = “16.2.2021”

can I add weeks also like using .AddWeeks same as .AddDays and .AddMonths ??

@Sakshi_Jain - please review the below statements here…date is 16.2.2021 only…This is what happens when you open the multiple queries on the same topic…you lose track here…