DateTime in C# winforms

Multi tool use


DateTime in C# winforms
Actually I am working with .CSV file. I have taken all data of .CSV file into DataTable.
code is below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Revision1
{
public partial class Form1 : Form
{
DataTable datable = new DataTable();
public Form1()
{
InitializeComponent();
}
private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select .CSV ffile";
ofd.ShowDialog();
txtboxpath.Text = ofd.FileName;
}
private void operatebtn_Click(object sender, EventArgs e)
{
string filePath = txtboxpath.Text;
StreamReader sr = new StreamReader(filePath);
string line = sr.ReadLine();
string value = line.Split(',');
DataRow row;
foreach (string dc in value)
{
datable.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == datable.Columns.Count)
{
row = datable.NewRow();
row.ItemArray = value;
datable.Rows.Add(row);
}
}
}
private void generatecsvbtn_Click(object sender, EventArgs e)
{
}
}
}
Till now everything is fine.
The google drive link of .CSV file click for.csv file
Now I am checking the below condition which is also inside operatebtn_Click method after last while loop:
DateTime dt = DateTime.ParseExact(datable.Rows[0][1].ToString(),"dd-MM-yy HH:ss", CultureInfo.InvariantCulture);
string s = dt.ToString();
string s1 = datable.Rows[0][1].ToString();
if(s==s1)
{
//code here
}
But the condition is giving false value. How I modify my code so that the condition will be true?
When I am debugging through breakpoint I am getting the following value:
dt = 11/30/2017 10:00:00 AM
s = 30-NOV-2017 10:00:00 AM
s1 = 30-11-2017 10:00
No, I was not aware of that
– Punam
16 hours ago
It makes working with them much easier and safer with dates, decimals etc.
– VDWWD
16 hours ago
Okay, I am checking out the library
– Punam
16 hours ago
I have no idea how to work with this library.
– Punam
16 hours ago
2 Answers
2
Your problem has nothing to do with the CSV file or the datatable.
You are parsing a string
with an exact specific format into a DateTime
. You are then converting that DateTime
back to a string
but with no specific format info, so the conversion uses the default format of the culture to format the DateTime
to a string
.
string
DateTime
DateTime
string
DateTime
string
If you want to get the same result, use the same format when calling ToString
:
ToString
string s = dt.ToString("dd-MM-yy HH:ss");
You should convert all the strings to be compared to DateTimes
and compare those. After all you want to compare dates, not texts. The DateTime
structure itself does not store the dates as formatted and is therefore neutral in this respect. According to the original documentation
DateTimes
DateTime
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar) [...]
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Did you know there are specialized libraries for parsing CSV files, like joshclose.github.io/CsvHelper
– VDWWD
16 hours ago