# Convert String to Datetime and sort a datatable

**URL:** <https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672>\
**Category:** Activities\
**Tags:** date, datatable, datetime, studio\
**Created:** [October 27, 2023, 2:52pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672 "2023-10-27T14:52:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanmay\_V\_Chetule](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tanmay\_V\_Chetule](https://forum.uipath.com/u/Tanmay_V_Chetule)\
**Post date:** [October 27, 2023, 2:52pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/1 "2023-10-27T14:52:46Z")

</div>

I have a datatable with column ‘Date’ in a string format. I need to convert that column into datetime or date format and sort the values in descending order. Once they are sorted, I need to get the difference in dates of the top 2 values. Please help me with the solution.

---

<div class="post-metadata">

**Author:** ![Praveen\_Mudhiraj](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/praveen_mudhiraj/32/420424_2.png) [@Praveen\_Mudhiraj](https://forum.uipath.com/u/Praveen_Mudhiraj)\
**Post date:** [October 27, 2023, 3:12pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/2 "2023-10-27T15:12:56Z")

</div>

can you provide the sample input and expected output

@Tanmay_V_Chetule

---

<div class="post-metadata">

**Author:** ![Tanmay\_V\_Chetule](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tanmay\_V\_Chetule](https://forum.uipath.com/u/Tanmay_V_Chetule)\
**Post date:** [October 27, 2023, 3:43pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/3 "2023-10-27T15:43:34Z")

</div>

![image](https://global.discourse-cdn.com/uipath/original/4X/4/5/8/4580d68cd9f9905db4e4a27cdda1bcd5b8dfc277.png)  
This will be the input datable, Actually it is read from excel so cant convert the datatype, date column will be string only.

Then we need to sort it in descending order acc to date and get the difference between top two dates… So here top two dates will be - 10/27/2023 and 5/19/2023. and then get the difference between them.

---

<div class="post-metadata">

**Author:** ![postwick](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/postwick/32/227083_2.png) [@postwick](https://forum.uipath.com/u/postwick)\
**Post date:** [October 27, 2023, 4:16pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/4 "2023-10-27T16:16:35Z")

</div>

Add Data Column to create the datetime column.

For Each Row in Data Table, assign newColumnName = DateTime.ParseExact(CurrentRow(“stringDateColumn”).ToString,“_format_”,System.Globalization.CultureInfo.InvariantCulture)

Sort Data Table on the newColumnName

---

<div class="post-metadata">

**Author:** ![Tanmay\_V\_Chetule](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@Tanmay\_V\_Chetule](https://forum.uipath.com/u/Tanmay_V_Chetule)\
**Post date:** [October 27, 2023, 4:59pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/5 "2023-10-27T16:59:48Z")

</div>

> [@postwick](#):
>
> DateTime.ParseExact(CurrentRow(“stringDateColumn”).ToString,“_format_”,System.Globalization.CultureInfo.InvariantCulture)

The format I’ve added is MM/DD/YYYY, Sample date - 4/19/2023

The error is string was not recognised as a valid datetime

---

<div class="post-metadata">

**Author:** ![supermanPunch](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/supermanpunch/32/133652_2.png) [@supermanPunch](https://forum.uipath.com/u/supermanPunch)\
**Post date:** [October 27, 2023, 5:15pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/6 "2023-10-27T17:15:02Z")

</div>

> [@Tanmay\_V\_Chetule](#):
>
> MM/DD/YYYY

Hi @Tanmay_V_Chetule ,

Could you try changing the format to `MM/dd/yyyy` and check ?

Or we could try instead of the `DateTime.ParseExact` method, we could directly try with `CDate()`

```auto
CurrentRow("NewDateColumn") = CDate(CurrentRow("Date").ToString)

```

---

<div class="post-metadata">

**Author:** ![Palaniyappan](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/palaniyappan/32/524095_2.png) [@Palaniyappan](https://forum.uipath.com/u/Palaniyappan)\
**Post date:** [October 27, 2023, 5:16pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/7 "2023-10-27T17:16:43Z")

</div>

Use this expression in assign activity

```auto
dt = (From r In dt Order By 
If(r(“Date”).ToString.Trim.Length.Equals(10), 
DateTime.ParseExact(r(“Date”).ToString.Trim, “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture), DateTime.ParseExact(r(“Date”).ToString.Trim, “M/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture)) Descending Select r).CopyToDataTable

```

Cheers @Tanmay_V_Chetule

---

<div class="post-metadata">

**Author:** ![postwick](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/postwick/32/227083_2.png) [@postwick](https://forum.uipath.com/u/postwick)\
**Post date:** [October 27, 2023, 5:58pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/8 "2023-10-27T17:58:17Z")

</div>

> [@Tanmay\_V\_Chetule](#):
>
> The format I’ve added is MM/DD/YYYY, Sample date - 4/19/2023
> 
> The error is string was not recognised as a valid datetime

MM is for month (must be two digits). dd is for day. yyyy is for year.

> **[Custom date and time format strings - .NET](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings)**
>
> Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [November 13, 2023, 2:06pm UTC](https://forum.uipath.com/t/convert-string-to-datetime-and-sort-a-datatable/664672/9 "2023-11-13T14:06:13Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
