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
Related
I have problem. I have MySQL database and I use DATE type for my date column. I want to save date without time only.
In database date is saved like this 06/02/1999 for example.
But when I try to take it with dapper
var test = connection.QueryFirstAsync<string>(#"SELECT BirthDate FROM Students");
Then it returns 06/02/1999 00:00:00
How can I fix that? I want string without that time that shouldn't even be there in first place.
Thank you very much for answers
Simple (maybe naive) solution is this one:
string date = "06/02/1999 00:00:00";
DateTime dateTime = DateTime.Parse(date);
DateOnly dateOnly = DateOnly.FromDateTime(dateTime);
Note: DateOnly applies to .NET 6, .NET 7 Preview 6
Here is my date time format displayed
Dr["DATE"] =========> Jun 27 2014 9:06PM
I want it to display like this one =========> 27.06.2014
Here is my code:
ltrlNotlar.Text = Dr["DATE"].ToString()
How to change first format to the second one?
Thankss
use DateTime to do the conversion for you
var dt = DateTime.Parse(Dr["DATE"].ToString());
ltrlNotlar.Text = dt.ToString("dd.MM.yyyy");
If Dr is a DataRow, you can this
ltrlNotlar.Text = dr.Field<DateTime>("DATE").ToString("dd.MM.yyyy");
I like using .Field because it does the casting for you. However this only works if the sql column is a Date or DateTime (or possibly DateTimeOffset). If you are storing your date as a string in the database, this won't work.
If the sql column DATE is nullable, make sure to change the generic argument to DateTime?.
you could also use something like this:
Convert.ToDateTime(SomeDateTime).ToString("MM.dd.yyyy");
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
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
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()