Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -6299,6 +6299,9 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-retrieve-the-name-of-the-chart-in-an-Excel-worksheet">How to retrieve the name of the chart in an Excel worksheet?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-convert-text-formatted-date-values-to-datetime-in-excel">How to convert text formatted date values to DateTime in an Excel?</a>
</li>
</ul>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
title: Convert text formatted date values to DateTime in Excel | Syncfusion
description: Code example to convert text formatted date values to DateTime in Excel workbook using Syncfusion .NET Excel library (XlsIO).
platform: document-processing
control: XlsIO
documentation: UG
---

# How to convert text formatted date values to DateTime in an Excel?

The following code examples demonstrate converting text formatted date values to DateTime in Excel workbook using C# (Cross-platform and Windows-specific) and VB.NET.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Program.cs,180" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Open the input workbook
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx"));

//Access the first worksheet
IWorksheet worksheet = workbook.Worksheets[0];

//Get the used range to iterate through populated cells
IRange used = worksheet.UsedRange;

//Set culture and parsing styles for interpreting text dates
CultureInfo culture = new CultureInfo("en-IN");
DateTimeStyles styles = DateTimeStyles.None;

//Iterate through the used range and convert text formatted dates to DateTime
for (int row = used.Row; row <= used.LastRow; row++)
{
for (int col = used.Column; col <= used.LastColumn; col++)
{
IRange cell = worksheet[row, col];
DateTime date;

//Log if the cell already contains a true DateTime
if (cell.HasDateTime)
{
Console.WriteLine(cell.DateTime);
}
//Try parsing text using the specified culture and assign DateTime back to the cell
else if (DateTime.TryParse(cell.Value, culture, styles, out date))
{
cell.DateTime = date;
}
}
}

//Saving the workbook
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Open the input workbook
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx"));

//Access the first worksheet
IWorksheet worksheet = workbook.Worksheets[0];

//Get the used range to iterate through populated cells
IRange used = worksheet.UsedRange;

//Set culture and parsing styles for interpreting text dates
CultureInfo culture = new CultureInfo("en-IN");
DateTimeStyles styles = DateTimeStyles.None;

//Iterate through the used range and convert text formatted dates to DateTime
for (int row = used.Row; row <= used.LastRow; row++)
{
for (int col = used.Column; col <= used.LastColumn; col++)
{
IRange cell = worksheet[row, col];
DateTime date;

//Log if the cell already contains a true DateTime
if (cell.HasDateTime)
{
Console.WriteLine(cell.DateTime);
}
//Try parsing text using the specified culture and assign DateTime back to the cell
else if (DateTime.TryParse(cell.Value, culture, styles, out date))
{
cell.DateTime = date;
}
}
}

//Saving the workbook
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
Using excelEngine As New ExcelEngine()

Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx

'Open the input workbook
Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx")

'Access the first worksheet
Dim worksheet As IWorksheet = workbook.Worksheets(0)

'Get the used range to iterate through populated cells
Dim used As IRange = worksheet.UsedRange

'Set culture and parsing styles for interpreting text dates
Dim culture As New CultureInfo("en-IN")
Dim styles As DateTimeStyles = DateTimeStyles.None

'Iterate through the used range and convert text formatted dates to DateTime
For row As Integer = used.Row To used.LastRow
For col As Integer = used.Column To used.LastColumn

Dim cell As IRange = worksheet(row, col)
Dim parsedDate As DateTime

'Log if the cell already contains a true DateTime
If cell.HasDateTime Then
Console.WriteLine(cell.DateTime)

'Try parsing text using the specified culture and assign DateTime back to the cell
ElseIf DateTime.TryParse(cell.Value, culture, styles, parsedDate) Then
cell.DateTime = parsedDate
End If

Next
Next

'Saving the workbook
workbook.SaveAs("Output.xlsx")

End Using
{% endhighlight %}
{% endtabs %}

A complete working example in C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/DateTime/.NET/TextToDateTimeConverter">this GitHub page</a>.