How to convert string to dateTimePicker format in C#? - c#

I am fetching data from database in datatable's object called dt. I want to convert that string in to dateTimePicker's format.
I have tried this code but its not working .
dtpPlanDate.Value = DateTime.ParseExact(dt.Rows[0]["PlanSDate"].ToString(), "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
it gives me this error
String was not recognized as a valid DateTime
I also tried another format:
dtpPlanDate.Value = Convert.ToDateTime(dt.Rows[0]["PlanSDate"].ToString());
it gives me error
Could not determine the order of year, month, and date from
'mm/dd/yyyy'.
I also tried one more format
dtpPlanDate.Value =DateTime.Parse(dt.Rows[0]["PlanSDate"].ToString());
it gives me below error
Could not determine the order of year, month, and date from
'mm/dd/yyyy'.
please let me know right format

You can convert your parameter while fetching from the database :
use this :
CONVERT(CHAR(10), PlanSDate, 101) AS PlanSDate

Related

Date is not taking proper format as Expected (dd/MM/yyyy)

I want to insert date into database in dd/MM/yyyy format. For that I have written like below:
drExpInfo[0]["CHEQUE_DT"] = string.IsNullOrWhiteSpace(e.Record["CHEQUE_DT"].ToString())
? DBNull.Value : (object)Convert.ToDateTime(e.Record["CHEQUE_DT"]);
And it is working perfectly fine on my local machine, but on my server it is taking format as dd/MM/yyyy hh:mm:ss. So how to set the same format there too. Kindly suggest.
Use DateTime.ToShortDateString:
drExpInfo[0]["CHEQUE_DT"] = string.IsNullOrWhiteSpace(e.Record["CHEQUE_DT"].ToString())
? DBNull.Value : (object)Convert.ToDateTime(e.Record["CHEQUE_DT"]).ToShortDateString();
But, I suggest you keep the time part of your date (without using the ToShortDateString as it will insert a time set to midnight, cf. highlighted text in Oracle documentation below) when inserting and get the format you want (without time) when you're using the date.
From Oracle documentation:
Oracle Database automatically converts character values that are in the default date format into date values when they are used in date expressions.
If you specify a date value without a time component, then the default time is midnight. If you specify a date value without a date, then the default date is the first day of the current month.
Oracle Database DATE columns always contain fields for both date and time. If your queries use a date format without a time portion, then you must ensure that the time fields in the DATE column are set to midnight. You can use the TRUNC (date) SQL function to ensure that the time fields are set to midnight, or you can make the query a test of greater than or less than (<, <=, >=, or >) instead of equality or inequality (= or !=). Otherwise, Oracle Database may not return the query results you expect.
So you can convert you're date in any format you want in c#, removing the time part, Oracle will automatically set the time part of your date as midnight (00:00:00).
You can set CultureInfo;
var cultureInfo = new CultureInfo("en-GB", false).DateTimeFormat;
string result = Convert.ToDateTime(e.Record["CHEQUE_DT"], cultureInfo).ToString();
Check this Discussion
Here you can find two or more ways to setting date format while insert records into database.
insert into sampleDate(Start_Date) values (to_date('25-Jun-2017','YYYYMMDD'))
Try:
public static DateTime? DateFromString(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}
else
{
return DateTime.ParseExact(dateString, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
}
DateTime? date=DateFromString(e.Record["CHEQUE_DT"]);
drExpInfo[0]["CHEQUE_DT"] = date==null? DBNull.Value : (object)date);

Date format when importing from MySQL Date into the Textbox C#.NET

So I'm populating a DATE field in MySQL table from the DateTimePicker and on the other form I need to show the date in the textbox in "dd.MM.yyyy" format, but for some reason it populates the textbox in "MM/dd/yyyy HH:MM:SS" format.
I'm populating it simply like this:
tbDate.Text = dt.Rows[0][14].ToString();
How do i change the format?
Try following code
tbDate.Text = ((DateTime)dt.Rows[0][14]).ToString("dd.M.yyyy");
I need to show the date in the textbox in "dd.MM.yyyy" format
you need to first cast the value into DateTime type and then pass your Custom Date Format String to the ToString() function
Try This:
tbDate.Text = ((DateTime) dt.Rows[0][14]).ToString("dd.MM.yyyy");
Instead of changing the format in your C# code, directly fetch the date in the required format from database.
You can use the following query to fetch the date in dd.MM.yyyy format
select DATE_FORMAT(date_column,'%d.%m.%Y') FROM table_name;
Please refer this SQLFiddle Example
And then you can use your following code as it is,
tbDate.Text = dt.Rows[0][14].ToString();
Hope this helps :)

To Get Data that its type is Date in C#

In my SQL database, I have BeginDate column of type date (so I want to get only day,month,year).
I add data into database in C#.
I look at Table_Data and everything is fine (for example BeginDate = 05/04/2014).
But when I get this data from database in C# with
string a= dt.Rows[i]["BeginDate"].ToString()
result is: a= 05/04/2014 12:00:00 AM
Why?
I can remove hour in string a with any methods. But why? I want to get only 05/04/2014 from database.
This is a display problem, not a data problem. Use the Formatting features of DateTime.ToString() to display the date in the format that you want.
If the data is coming out of the database in string form, use DateTime.Parse on it first.
Reference
Standard Date and Time Formats
Custom Date and Time Formats

how to get the day from the date in sql and C#?

Does anyone know how to get the day part from a datetime? For example, I have a datetime 2013-11-30 11:53:02.360. I just need the 30 from the datetime. How do I get it using sql and C#?
Update: Maybe I didn't explain it right. Here is the scenario:
We have a record in our table which has start date and end date, for example:
2013-10-03 11:53:02.360
2013-10-08 14:36:01.090
In my web page I have an event that only occurs if the current day is between 3-8, I don't care about the year and month. So if the current date is 2014-01-05, the event will occur.
Now I question is, if I have 2013-09-30 ... as start date in the table, how do I check to make sure the event will still occur? Basically I think I need to check current date is fall into the same rage of the dates defined in the table.
DateTime.Parse("2013-11-30 11:53:02.360").Day
In T-SQL (assuming you're talking about SQL Server when you say SQL):
SELECT DAY(GETDATE())
In C#:
int day = DateTime.Today.Day;
if your date Type is DateTime and contains 2013-11-30 11:53:02.360
Try This:
String Day=dt.ToString("dd"); //it contains Day-> 03
( or )
if your date Type is String and contains 2013-11-30 11:53:02.360
Try This:
String strDate = "2013-11-03 11:53:02.360";
DateTime dt = DateTime.ParseExact(strDate, "yyyy-MM-dd HH:mm:ss.fff",
System.Globalization.CultureInfo.InvariantCulture);
String Day=dt.ToString("dd"); //it contains Day-> 03

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

Categories

Resources