C# DateTime Convert from String - c#

I am reading excel file in my C# .net web application and storing that value in Database. Ony of field in Excel is DateTime. I am storing it in string
string tran_time = Convert.ToString(odr[5]); //tran_time is "03-11-2012 16:08:43"
and then convert it in in DateTime and store it in Database (SQL Server 2008)
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal = DateTime.ParseExact(tran_time, "dd-MM-yyyy HH:mm:ss", culture);
But the Value Being stored in Database is in format
2012-05-11 13:40:23.000 (yyyy-mm-dd)
and the value in Excel is 05-11-2012 13:40:23 (dd-mm-yyyy)
Date & Month is get replaced.
My Question is How can i store it in Database in Format (YYYY-MM-DD HH:MM:SS:FFF)

But the Value Being stored in Database is in format
No it isn't (assuming that you are using a DATETIME column type). This is just what the SQL tools show you.
A DateTime instance, either in a database or in C# does not have an associated format. It only gets formatted when displayed to the user.

Or use yyyy-MM-dd format. Works like a charm:
string myString = dateVal.ToString("yyyy-MM-dd");

Use dd-mm-yyyy instead of dd-MM-yyyy.

Related

Read Date from SQL Server

How do I read a date from SQL Server to a datetime variable in C# ?
DateTime sDate = (read["notDate"]);
I get error
Cannot convert object to a system.datetime [are you missing a cast?]
Edit: the SQL Server uses a Date datatype for this column.
Then how will format the sDate to UK format - I was looking at
format("dd/MM/yyyy")
Using ASP.NET, C#, SQL Server Express.
Thanks for your help.
you need to cast it into a date.. this should do the job
DateTime sDate = (DateTime)read["notDate"];
To then format it into a dd/MM/yyyy you can do
string formattedDate = sDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

OleDb return Date Column value with time

I have Excel File(.xls) which contain date column without time value. I am trying to read this file and put it in datatable with connection string as below:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test\Desktop\1.xls; Extended Properties="Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text"
Excel File Data
OleDb return datem value with time. 7/20/1995 12:00:00 AM
DataTable Data
But i want datem column value without time. Anyone have idea what i have to do to solve my problem.
Thanks.
.Net framework's DateTime consist of Date and Time. You can't really separate Time part from DateTime, instead you can use custom format for displaying records.
string str = DateTime.Today.ToString("MM/dd/yyyy");
I assume it's just a display issue in the DataSet visualizer. Actually it's a DateTime column which has no inherent format. So if you want to output a DateTime only with the date part:
string date = dt.ToShortDateString(); // or dt.ToString("d")
The Short Date ("d") Format Specifier

Date format of sql from regional settings

I am reading date from the sql table. In sql table date time in the format how client set in regional format. Because of this reading date and splitting that and taking to a format what we want is becoming difficult. Is there any way to restrict sql date format from regional settings date format (means sql not suppose to take regional settings date format).
Now i got the answer:
string Date = Convert.ToDateTime(dateTimeString).ToString("yyyy-MM-dd");
string time = Convert.ToDateTime(dateTimeString).ToString("hh:mm:ss");
//dateTimeString--->my dateTime value from sql database
If you write datetime fields using ISO 8601 there should be no problem.
That way, dates get formatted like "2011-09-26T12:04:00", so there's no misunderstanding possible.

Propagate a DateTime column of a DataSet to database

I have a DataSet that has a timestamp (i.e. Datetime.Now ) recording the date and time when a row is added to the DataSet in memory. I will later save (or propagate) all these rows to the SQLCE database where the DataSet's timestamp will be propagate to a DateTime column in the database table.
It works fine and datetime comparison is also ok:
dataView1 = new DataView(
dt,
"DataTime >= '6/9/2011 5:00:20 PM'",
"Data_ID ASC",
DataViewRowState.CurrentRows);
The above code works ok, but I worry if the program runs on different computer (i.e. another language of Windows) the Datetime.Now format would have a different format, or if I compare data that is record from a different computer, the different format of the DateTime in the database will causes it to fail. Would this problem happen? Or is there a safer way of doing this?
String datetime = String.Format("{0:G}", ur_datetime_variable);
will give you string in M/d/yyyy h:mm:ss tt format and then convert the datetime string to DateTime
here's a link http://www.csharp-examples.net/string-format-datetime/ and there's everything bout datetime formating.
Should you not use CultureInvariant
DateTimeFormatInfo.InvariantInfo
when parsing dates

problem with date in sql CE

i have sqlCE DataBase, i have Tdate field (datetime)
i notice that his format is: MM/DD/YYYY
in my C# program i work with date in: DD/MM/YYYY format.
how i can insert to my sqlCE data base my C# format ?
i have Tdate field (datetime) i notice
that his format is: MM/DD/YYYY
No, the datetime field doesn't have any format. The format is determined when the datetime value is converted into a string after reading it from the database.
The same works when you insert a datetime value in the database, you use a datetime value, not a string. If you supply a string value to the database, it will try to parse it into a datetime value. Whatever format you use when you insert the datetime value, that doesn't affect how the value is returned when you read it, as only the value is stored in the database not the format.
When you insert the value you should use a parametererized query so that you supply the date as a DateTime value, not as a formatted string in the query.
When you read the data from the database, you get a DateTime value. You can either set the culture of the application to control how the default conversion from DateTime to string is done, or use a specific culture or format when you convert it.
Maybe there's some function, i'm not sure. My variant is:
string DateString = "25/10/2009";
DateTime dateTime = DateTime.Parse(DateString);
string sqlString = datetime.Month+"/"+dateTime.Day+"/"+dateTime.Year;
I think you don't need to worry about the Date Format.

Categories

Resources