Split date dd/mm/yy into variables day = dd, month = mm, year = mm

I am getting data from a spreadsheet. The cell type in the spreadsheet which contains the date is saved in date format.

Need to loop through the spreadsheet and for each date in format dd/mm/yy I need to get day = dd, month = mm, year =yy


ForEach row in DataTable

String S_date = row(“Sdate”).ToString
DateTime S_dt = DateTime.ParseExact(S_date,“dd/MM/yy”, System.Globalization.CultureInfo.InvariantCulture)
Int32 S_day = S_dt.Day
Int32 S_month = S_dt.Month
Int32 S_year = S_dt.Year

However, I get the below error when I try and assign S_dt


Hi @Aishwarya_Maggi

Can you please print the value row(“Sdate”).ToString and let us know here what exactly you are trying to parse.

The error occurs when the string is not in date format.


When date is 02/03/19 in dd/mm/yy


Try the below expression

DateTime.ParseExact(S_date,“dd/MM/yy H:mm:ss”, System.Globalization.CultureInfo.InvariantCulture)

I’m still getting the same error

Can you please try the below format once -

dd/MM/yy hh:mm:ss

If you still getting the error, then the string is not in the format dd/mm/yy as you said.

Try this out : Test.xaml (6.0 KB)


date is defined as String with a value of, for instance, “25/12/19”.

day, month and year is defined as String.
Substring(string-index, string-lenght)

day = date.Substring(0,2) = 25
month = date.Substring(3,2) = 12
year = date.Substring(6,2) = 19

Hi @Aishwarya_Maggi,

Try below code
DateTime S_dt= Convert.ToDateTime(S_date)
Int32 S_day = S_dt.Day
Int32 S_month = S_dt.Month
Int32 S_year = S_dt.Year

Arivu :slight_smile:


This makes ReadRange already read it as a DateTime (unless PreserveFormat is checked).
All of the above solutions are essentially doing:
Get cell value (which is a DateTime) as a string
Parse it to DateTime (with fixed format, aside of Arivu’s answer)
Get elements out

There are unneeded steps there, which are actually dangerous - tostring will output in invariant culture which is month first. It worked here because day was low enough to also function as a month.
Only @arivu96 answer will not break your data, but it still has one line too much (the one quoted). Convert checks the type and if it is the target type it will straight up return it, otherwise will delegate to parse.
If you pass your cell directly to convert call effect will be the same, but without the danger of DateTime -> String -> DateTime roundtrip parsing.
Do note that all of these will fail if the cell is empty.