String to Date with current Time - c#

I want to convert date entered int String to DateTime type
I use the following
DateTime date = Convert.ToDateTime(dateText);
Let say the dateText=5/20/2014 DateTime date become 5/20/2014 and 12:00:00 AM. I I want the current time of the day to be picked up instead of 12:00:00 AM.
Is there a workaround?

After you do:
DateTime date = Convert.ToDateTime(dateText);
Then do:
DateTime fullDate = new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
...or as suggested by Ulugbek Umirov:
DateTime fullDate = date.Add(DateTime.Now.TimeOfDay);

Try this :
DateTime date = Convert.ToDateTime(dateText + " " + DateTime.Now.TimeOfDay);

Related

Join date and time in c#

I have a win form c# SQL app that stores date in one column and time in the another.
There is only one date time picker on my form and I want to display both date and time values (which are from two separate columns)..
So far this is what I've done
Datetime final = datetime. Parse exact (date + " " + time , "MM/dd/yyyy hh:mm tt", cultureinfo. Invariant culture);
But it throws " string was not recognized as valid datetime" exception on the above line.
If date and time are DateTime variables, you can combine them with date arithmetic:
DateTime date=...;
DateTime time = ...;
DateTime finalDate = date.Date + time.TimeOfDay;
If they are strings, you can parse them to DateTime and TimeSpan variables:
DateTime date=DateTime.ParseExact(dateString,dateFormat,CultureInfo.InvariantCulture);
TimeSpan time = TimeSpan.ParseExact(timeString,timeFormat,CultureInfo.InvariantCulture);
DateTime finalDate = date.Date + time;
This is possible because you can add a DateTime and a TimeSpan value to get a new DateTime value
You can use TimeSpan.Parse to parse
DateTime newDateTime = date.Add(TimeSpan.Parse(time));
string d = DateTime.Now.Date.ToString("dd/MM/yyyy");
string t = DateTime.Now.ToString("h:mm");
var ts = TimeSpan.ParseExact(t, #"h\:mm",CultureInfo.InvariantCulture);
DateTime result = DateTime.ParseExact(d, "dd/MM/yyyy", CultureInfo.InvariantCulture)+ts;
Hope this helps,
Thanks.

add Seconds to a date and get new date

How to Get new Date provided a date and number of seconds passed from that date.For Example 03-12-2014 12:30:02 AM is the date and 124 are the seconds lapsed then new date time should be 03-12-2014 12:32:06 AM
Try something like:
string strDate = "03-12-2014 12:30:02 AM ";
DateTime date;
if (DateTime.TryParse(strDate , out date))
{
date = date.AddSeconds(124);
}
Use addition operator with a TimeSpan:
var date = myDate + TimeSpan.FromSeconds(124);

How to get differences of dates

I am making a web page in that I have used Ajax calendar to pick two date like TO date and From date and I also have a Textbox of total days.
So when user selects to and from dates, the difference of these dates is displayed in the textbox. So how can I find the difference of these dates..?
I set the format like dd/MM/yyyy.
e.g.
one textbox has: 20/04/2012
second has : 02/05/2012
So, please find difference on these ?
Thanks in Advance....
Mitesh
Substraction operator (-) works on DateTime
DateTime to_datetime = DateTime.ParseExact(to_textbox.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
DateTime from_datetime = DateTime.ParseExact(from_textbox.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
Timespan result = to_datetime - from_datetime;
You can use it as
textBox1.Text = (to_datetime - from_datetime).TotalDays.ToString();
Convert your textbox values to date using:
DateTime dt1 = DateTime.ParseExact(textbox1.Text, "d/M/yyyy", CultureInfo.InvariantCulture);
DateTime dt2 = DateTime.ParseExact(textbox2.Text, "d/M/yyyy", CultureInfo.InvariantCulture);
Use TimeSpane
TimeSpan ts = dt1.Subtract(dt2);
Console.Write(ts.TotalDays);
textBox3.Text = ts.TotalDays;
Assuming C# code: DateTime support "-" that results in TimeSpan object.
DateTime nowTime = DateTime.Now;
DateTime yesterday = nowTime.AddDay(-1);
TimeSpan diff = nowTime - yesterday;
DateTime date1 =DateTime.ParseExact("20/04/2012","d/M/yyyy",null);
DateTime date2 = DateTime.ParseExact("02/05/2012", "d/M/yyyy", null);
TimeSpan datediff = date2 - date1;
Response.Write(datediff.ToString());

C# DateTime to UTC Time without changing the time

How would I convert a preexisting datetime to UTC time without changing the actual time.
Example:
DateTime dateTime = GetSomeDateTime(); // dateTime here is 3pm
dateTime.ToUtcDateTime() // datetime should still be 3pm
6/1/2011 4:08:40 PM Local
6/1/2011 4:08:40 PM Utc
from
DateTime dt = DateTime.Now;
Console.WriteLine("{0} {1}", dt, dt.Kind);
DateTime ut = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
Console.WriteLine("{0} {1}", ut, ut.Kind);
Use the DateTime.SpecifyKind static method.
Creates a new DateTime object that has the same number of ticks as the specified DateTime, but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.
Example:
DateTime dateTime = DateTime.Now;
DateTime other = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
Console.WriteLine(dateTime + " " + dateTime.Kind); // 6/1/2011 4:14:54 PM Local
Console.WriteLine(other + " " + other.Kind); // 6/1/2011 4:14:54 PM Utc
You can use the overloaded constructor of DateTime:
DateTime utcDateTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, DateTimeKind.Utc);
Use the DateTime.ToUniversalTime method.

To get time along with Date from DateTimePicker

I have a datetimepicker whose format is short.I have to get time along with date in code behind.Now 12:00:00 am is inserting along with date.Instead i need to insert current time along with date picked from datetimepicker.Can anybody help?
Create a new DateTime using the date from your input and the time from DateTime.Now.
DateTime GetDateAndCurrentTime(DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
}
The actual DateTime.Value from the picker is a full DateTime object with the current time.
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime dt = dateTimePicker1.Value;
// or
string s = string.Concat(dt.ToShortDateString(), DateTime.Now.ToShortTimeString());
}
Full DateTime:
dateTimePicker1.Value;
'08/21/2011 14:42:11'
Short DateTime:
dateTimePicker1.Value.Date;
'08/21/2011 00:00:00'
The minimum value
'08/21/2011 00:00:00'
The maximum value
'08/21/2011 23:59:59'
Usage:
var firstDate = dtpFrom.Value.Date; // 0:00:00
var secondDate = dtpTo.Value.Date.AddSeconds(86400 - 1); //23:59:59 - 86400 is 24 hours
var list = Services.GetListForFilter(firstDate, secondDate);

Categories