Get only the date from a datetime in c# - c#

I'm trying to pass a date in a viewbag like this
DateTime? DueDate = this.dbContext.OBProcessMonitors.Where(o => o.EmployeeID
== GlobalVariables.EmployeeID).First().DueDate;
ViewBag.DueDate = DueDate;
WHen i call the viewbag in razor #viewbag.duedate i get the date and time. How can I get just the date out of it?

Use 'DueDate.Value.Date' to get just the date of a datetime.

I assume that you need a date string, not a date object. So do following
To get short date string: like "6/15/2009" in en-US
Value.ToShortDateString()
Or to get long date string: like "Monday, June 15, 2009"
Value.ToLongDateString()
Or using custom format
Value.ToString("d")
Check other formats here: https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

You can convert DateTime to string and format it however you want to:
ViewBag.TheDate = yourobject.DueDate.ToString("dd/MM/yyyy") //or,
ViewBag.TheDate = yourobject.DueDate.ToString("MM/dd/yyyy") //or,
ViewBag.TheDate = yourobject.DueDate.ToString("yyyy/MM/dd") //etc...

Related

How convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 3 years ago.
I am doing an asp.net mvc project and I need to convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second
You can use DateTime.ParseExact, here is an example :
http://net-informations.com/q/faq/stringdate.html
Finally, it should look like this :
string s = "06/22/2019 00:00:00";
DateTime myDate = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
Debug.WriteLine(myDate.ToString("MM/dd/yyyy"));
You can do this:
var dateString = "06/22/2019 00:00:00";
var datePart = dateString.Split(' ')[0];
var date = DateTime.Parse(datePart);
Though remember that DateTime will still have a default value for the time (12:00 AM), if you want the Date part only from the object, use date.Date which will return an instance with the default time (mentioned earlier).
DateTime contains default Time even if you access DateTime.Date. You can achieve format of date by converting Date into string.
Something like,
DateTime myDate = DateTime.ParseExact("06/22/2019 00:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string dateInFormat = $"{myDate.Year}/{myDate.Month}/{myDate.Day}";
POC : .net Fiddle
You convert the string to a DateTime object and then to display just the date portion you can use ToShortDateString like this:
var myDateTime = DateTime.Parse( "06/22/2019 00:00:00") //presumably use a variable here instead.
var date = myDateTime.ToShortDateString();
How you want to display this can be done using the CultureInfo part as shown here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

Format String Value As MM/dd/YYYY

I am querying SharePoint and my dates are returned as YYYY-MM-dd 16:27:12 - for display only I tried to modify the syntax to be
string dateInfo = xNode.Attributes[“Altered”].Value;
sb.Append(String.Format(“{0:MM/dd/yyyy}”, dateInfo));
However that is not formatting the date time. What do i need to change so that the date/time is returned in my required format?
My desired format is mm/dd/yyyy 4:27:12 12 hour format so
You have to parse it to DateTime first:
string dateInfo = xNode.Attributes["Altered"].Value;
DateTime dt = DateTime.Parse(dateInfo);
sb.Append(String.Format("{0:MM/dd/yyyy}", dt));
Try, like:
var result = DateTime.Parse(dateInfo).ToString("MM/dd/yyyy");

How to retrieve formatted version of date from date picker backend

I have a date picker and time picker I am using in my iOS application (using xamarin) and I am trying to retrieve the date and time selected by the user to use elsewhere in the code. My problem is that I am not sure how to retrieve just the date or just the time. For example I have this following code to retrieve the date:
// not getting proper date format!
var selectedDate = ContactDatePicker.Date.ToString ();
var selectedTime= ContactTimePicker.Date.ToString ();
Console.WriteLine ("Here: {0}, {1}", selectedDate, selectedTime);
But it outputs the entirety of the of the date and time for each variable like so:
Here: 2016-05-24 15:18:50 +0000, 2016-05-24 15:18:50 +0000
I would like to get something like 2016-05-24 for date or something like 15:18:50 for time. I realize I can use regex for this but I was wondering if there is a simple way to format dates.
Since UIDatePicker.Date returns nsdate you could convert it to DateTime first and supply required format to ToString method.
var dateTime = DateTime.SpecifyKind(ContactTimePicker.Date, DateTimeKind.Unspecified);
var selectedDate = dateTime.ToString("yyyy-MM-dd");
var selectedTime= dateTime.ToString("HH:mm:ss");
If ContactDatePicker.Date type is DateTime you can use ToShortDateString() and ToShortTimeString() or ToLongTimeString:
var selectedDate = ContactDatePicker.Date.ToShortDateString();
var selectedTime = ContactTimePicker.Date.ToLongTimeString();
//Output:
2016-05-24
15:18:50
According to documentation of UIDatePicker, Date property returns a NSDate, but there are an implicit conversion to DateTime, so you can do something like this:
DateTime date = ContactDatePicker.Date //Implicit conversion
var selectedDate = date.ToShortDateString();
var selectedTime = date.ToLongTimeString();
Thanks for the info guys I ended up using:
// explicitly convert NSDate to DateTime to change format
DateTime date = (DateTime)ContactDatePicker.Date;
DateTime time = (DateTime)ContactTimePicker.Date;
// able to overload ToString() method with argument to change format
var selectedDate = date.ToString ("d");
var selectedTime = time.ToString ("HH:mm:ss");

How to get textbox date formatted as a string?

I am using a textbox with text mode set as date. What I need is to arrange the date format to MM/dd/yyyy. I know if I use DateTime.Now, it gets today's date, but I cannot get the value of user's picked date if I do like this as there is error:
string dateOfBirth = DateTime.ToString("MM/dd/yyyy");
This is how I do:
string tmpDateOfBirth = tb_date.Text;
//This below line returns todays date and not the date that the user pick on the calendar.
string dateOfBirth = DateTime.Now.ToString("MM/dd/yyyy");
You are using today's date and formatting it... You need to format what has been entered in your textbox.
string dateOfBirth = tb_date.Text.ToString("MM/dd/yyyy");
EDIT : if you are getting 'best overloaded match error'
string dateOfBirth = Convert.ToDateTime(tb_date.Text).ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Okay, here i assume your TextBox1.Text contain valid date value. So below code might help you.
string dateOfBirth = Convert.ToDateTime(TextBox1.Text).ToString("MM/dd/yyyy",
System.Globalization.DateTimeFormatInfo.InvariantInfo);
You can directly get the selected value of DateTimePicker.
Try this:
string datetime = DateTimePicker1.Value.ToString("DateTimeFormat");
See this link http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value%28v=vs.110%29.aspx
How is the user picking the date?
If you have a DateTimePicker object, you can just type
string dateOfBirth = MyDateTimePicker.Value.ToString("MM/dd/yyyy");
EDIT
Textboxes are not meant to hold datetime objects. If you need to do so, you'll have to parse its text:
DateTime dt = DateTime.ParseExact("TextBox1.Text", "MM/dd/yyyy",
CultureInfo.InvariantCulture);

How to get today's date in mm/dd/yyyy format into a datetime variable in c#

I want today's date in mm/dd/yyyy format from a DateTime variable. How to get it?
I need to check this with some date variable so it must be also in date variable format?plz help
Ex: i need to get today date in mm/dd/yyyy format and i already date which is datetime datatype in mm/dd/yyyy format and i have to compare them
You should use DateTime.Today:
DateTime today = DateTime.Today; // As DateTime
string s_today = today.ToString("MM/dd/yyyy"); // As String
Edit: You edited your post to add another question, so here comes my edit to supply at least some sort of answer.
Update While you can use DateTime.Compare() you should use plain comparisson:
if(today < otherdate)
{
// Do something.
}
Alternatively, you can use DateTime-variables to check against other DateTime-variables using the DateTime.Compare() method. Both otpions will work and it comes down to preference and what you want to do with the result.
int result = DateTime.Compare(today, otherdate);
if(result < 0)
MessageBox.Show("Today is earlier than the 'otherdate'");
elseif(result > 0)
MessageBox.Show("Today is later than the 'other date'");
else
MessageBox.Show("Dates are equal...");
string datestring = DateTime.Now.ToString("MM/dd/yyyy");
MSDN say: Custom Date and Time Format Strings
To convert DateTime variable to string in the specified format:
DateTime d = ...;
string s = d.ToString("MM/dd/yyyy");
If you want to compare only date part of DateTime, not time part:
DateTime d1 = DateTime.Parse("10/10/2011");
DateTime d2 = DateTime.Parse("01/01/2011");
if (d1.Date > d2.Date)
{
// do the stuff
}
DateTime.Now.ToString("MM/dd/yyyy");
DateTime.Today.ToString("MM/dd/yyyy");
DateTime.Parse is what you are looking for...

Categories