How to get the time value in a stored procedure from a DateTimePicker value?
For Example:
I have a datetime picker value stored into my table as:
'2010-04-06 09:00:00.000'
'2010-04-07 14:30:00.000'
I have to get the date seperately using Convert(varchar,Date,3) as Date
I have got the time seperately using Convert(time,Time)
But it show the values as 9:00 or 14:30
I need it to show them as 9:00AM and 2:30PM instead of 14:30.
You can use yourDate.ToString("t");.
Also check other formats
try something like that:
date.ToString("hh:mm tt", CultureInfo.InvariantCulture);
to get current time you can use
now.toshorttime()
Related
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);
I wanted to save only date in my database. There is a table which holds dates in database and the column which holds date in type of "Date". Now I want to store date from UI,so I placed WPF DatePicker in UI which allows to select date, but whenever I try to get the data from datepicker it shows the date and time.But I want just the dates to be stored in database.
This is the thing i am doing. It is demo code by the way. Can t upload original code. But this explains the thing. you can see in the message box, it shows 14-10-2015 00:00:00 , i want this zeros to be removed.
The dateTime Picker has a property DisplayDate of Type DateTime. This type contains date and time information.
Just use picker.DisplayDate.Date this returns a DateTime value with the TimeOfDay component set to 00:00.00
Edit
Usually you use an SQL Statement to insert or update values in the database. You should use a parametrized SQL statement with an parameter of type DateTime. The SQL API will take care of the conversion form DateTime (.Net type) to your SQL Date type and strip all time information away. It is a good idea to set the time component to 00:00:00 however to avoid any strange "roundings".
Use ToShortDateString() at the end something like:
var date = datePicker.SelectedDate.Value.Date.ToShortDateString();
MessageBox.Show(date.ToString());
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 :)
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
In my ASP.NET (C#) project, I'm inserting some data into a database.
INSERT INTO USERS (username,join_date) VALUES('Ali',GETDATE());
Now when I'm fetching the date from the database, I'm using DateTime.
SqlDataReader r = Command.ExecuteReader();
r.Read();
myDate = r.getDateTime(0);
And when I'm inserting this in some DIV, I do this.
"<div>"+myDate.ToLongTimeString()+"</div>"
I get the correct date as in day/month/year, but the hour is always 12:00:00 AM.
How can I get the exact time?
make sure join_date is of type DATETIME and not DATE in the database
http://msdn.microsoft.com/en-us/library/ms186724.aspx
Using DATE as your column type means you are only storing the date not the time.
Therefore when you read the value back into a DateTime object the system has to initialise the time part - to midnight.
Convert the column to DATETIME and you'll get the time component stored as well.
For more information on the difference see the MSDN page on Date and Time Data Types and Functions