I want remove the time part. While using the given below code getting the out but like given : 4/27/2015 12:00:00 AM I want remove the 12:00:00 AM. I tried some methods. but it is not working.
Code:
DateTime start = Convert.ToDateTime(txtStart.Text);
DateTime end = Convert.ToDateTime(txtEnd.Text);
var date1 = start .Date;
var date2 = end .Date;
You can Following Code , the ToDateShortString() will Produce Date:
DateTime start = Convert.ToDateTime(txtStart.Text);
DateTime end = Convert.ToDateTime(txtEnd.Text);
var date1 = start.ToShortDateString();
var date2 = end.ToShortDateString();
try using format function and format to custom date format.
DateTime start = Convert.ToDateTime(txtStart.Text);
DateTime end = Convert.ToDateTime(txtEnd.Text);
var date1 = format(start.Date,"dd/MM/yyyy");
var date2 = format(end.Date,"dd/MM/yyyy");
Now tested and is working fine.use the format which u want.i assume u need "MM/dd/yyyy".
String test = DateTime.Now.ToString("dd.MM.yyy");
else
DateTime.Now.ToShortDateString ()
You can format the date.
String.Format("{0:M/d/yyyy}", start); // output: 4/27/2015
String.Format("{0:M/d/yyyy}", end); // output: 4/27/2015
Related
I have a string ("CompletionDate") which contains the value "2/28/2017 5:24:00 PM"
Now I have 2 variables (EDate and ETime). I want to assign the Date to EDate (i.e 2/28/2017) and Time to ETime (i.e. 5:24:00 PM).
How can I split the Date and Time from a single string.
Kindly Help.
My approach right now is like :
string CompletionDate = string.Empty;
string ProjectEDate = string.Empty;
string ProjectETime = string.Empty;
CompletionDate = "2017-03-29 12:58:00";
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd", CultureInfo.CreateSpecificCulture("en-us"));
DateTime dt1 = DateTime.ParseExact(CompletionDate, "HH:mm:ss", CultureInfo.CreateSpecificCulture("en-us"));
var ProjectEDate = dt.ToString();
var ProjectETime = dt1.ToString();
But its throwing exception that string is not in correct format. Kindly help
#Chris pointed one of your problems, but you have one more. You are passing full date time string and trying to treat it as date or time only, which is not true. Instead I suggest you to parse DateTime object with both date and time, and then take whatever you need from parsed object:
CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-us");
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd HH:mm:ss", enUS);
var ProjectEDate = dt.Date.ToString();
var ProjectETime = dt.TimeOfDay.ToString();
You need to specify the full format as same as the input string to parse method.
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CreateSpecificCulture("en-us"));
To get results you can use below methods available by default in DateTime.
dt.ToShortTimeString()
"12:58 PM"
dt.ToLongTimeString()
"12:58:00 PM"
dt.ToLongDateString()
"Wednesday, March 29, 2017"
dt.ToShortDateString()
"3/29/2017"
Or you can specify the format to ToString method.
dt.ToString("yyyy-MM-dd")
"2017-03-29"
dt.ToString("HH:mm:ss")
"12:58:00"
DateTime.ParseExact(CompletionDate, "yyy-MM-dd", ...
You are missing 4th 'y' in date format string:
"yyyy-MM-dd"
^
here
and:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
Why do you parse into DateTime and then convert to a string using ToString again? CouldnĀ“t you just simply use String.Split when all you want is to split the time from the day and you know the exact format?
var CompletionDate = "2017-03-29 12:58:00";
var tmp = CompletionDate.Split(' ');
var ProjectEDate = tmp[0];
var ProjectETime = tmp[1];
I have a variable testDate which contains value {01/02/2016 AM 12:00:00}. I need to get only date part from the set. For that I used below code:
var testDate = startDate.AddDays(i);
var testDateOnly = testDate.Date;
But it returns the same {01/02/2016 AM 12:00:00}.
The date variable will contain the date, the time part will be 00:00:00
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
// The date with time component
var testDate = startDate.AddDays(i);
// Get date-only portion of date, without its time (ie, time 00:00:00).
var testDateOnly = testDate.Date;
// Display date using short date string.
Console.WriteLine(testDateOnly.ToString("d"));
// OUTPUT will be 1/2/2016
var testDate = startDate.AddDays(i);
string dateOnlyString = testDate.ToString("dd-MM-yyy");
"dd-MM-yyy" this will be the Date Format of the output string, You can choose the format as per your requirements.
You can use the following also for formatting:
dateOnlyString = d.ToString("M/d/yyyy"); // "3/9/2008"
dateOnlyString = d.ToString("MM/dd/yyyy}"); // "03/09/2008"
// day/month names
dateOnlyString = d.ToString("ddd, MMM d, yyyy}"); // "Sun, Mar 9, 2008"
dateOnlyString = d.ToString("dddd, MMMM d, yyyy}"); // "Sunday, March 9,2008"
// two/four digit year
dateOnlyString = d.ToString("MM/dd/yy}"); // "03/09/08"
dateOnlyString = d.ToString("MM/dd/yyyy}"); // "03/09/2008"
You need to convert the Date in to String type using this,
var testDate = startDate.AddDays(i);
var testDateOnly = testDate.Date.ToShortDateString(); //string
or
var testDateOnly = testDate.Date.ToString("dd/MM/yyyy");
or
var testDateOnly = testDate.Date.ToString("d");
Check it here.
Find the more about Standard Date and Time Format Strings
You will get the dd-MM-yyyy format by doing this.
I have a date returned from database which includes even the time. i want to remove the time part of the string and send only the date. my code is as given below
DateTime Var = new DateTime();
Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]);
Var = Var.ToShortDateString();
DateTime Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]).Date; //only date part
string date = Var.ToShortDateString();
it will store only date in DateTime object
Var = Var.Date;
time will be 00:00:00
or you can store it as string:
var dateString = Var.ToShortDateString();
you can custom your date string format by using DateTime.ToSting("your format") method.
then the code will be like this.
DateTime Var = new DateTime();
Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]);
Var = Var.ToString("yyyy-MM-dd");
you can also use SubString() method to gain the date part of the datetime string.
I have various strings of type: 7/12/2012 12:02:39 AM
and would like to convert them all to just 7/12/2012 12:00:00 AM
Basically the date needs be the same, just the time must be set to 12:00:00 AM for all.
What is the best way to approach that? Except just looking for " " and replacing with 12:00:00 AM
First, parse them to a DateTime. Then you can use the Date property(0h) and parse it back to a String by using DateTime.ToString:
var oldDate = DateTime.Parse("7/12/2012 12:02:39 AM");
var usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
var newDateStr = oldDate.Date.ToString( usCulture );
Custom Date and Time Format Strings
only just noticed that 12 AM is midnight not highnoon
You can create new DateTime instance via constuctor:
var newDate = new DateTime(oldDate.Year, oldDate.Month, oldDate.Day, 12, 0, 0);
or by adding 12 hours to the date part of the DateTime(0h):
var newDate = oldDate.Date.AddHours(12);
Parse them as DateTime
Write them back to string using yourDateTime.Date.ToString(#"G", CultureInfo.CreateSpecificCulture("en-us"))
use DateTime.Parse and specify the CulterInfo
DateTime mydat = DateTime.Parse(myString, CultureInfo.InvariantCulture);
I have a list of dates as below
List<string> nameList = new List<string>();
nameList.Add("20120618PM");
nameList.Add("20120622PM");
nameList.Add("20120622AM");
nameList.Add("20120628PM");
nameList.Add("20120702PM");
nameList.Add("20120629AM");
nameList.Add("20120629PM");
I want to find MAXDATE and MIN DATE from the list .Please let me know how can i proceed.
Regards,
Channa
What format is that? "yyyyMMddtt"?
There is AM PM with date. There is no time to accompany AM/PM. So I am assuming AM is 00:00:00 and PM is 12:00:00
First correct your format then use this
List<DateTime> temp = nameList.Select(x =>
DateTime.ParseExact(x, "yyyyMMddtt", CultureInfo.InvariantCulture)).ToList();
Then
temp.Min("yyyyMMddtt");
temp.Max("yyyyMMddtt");
If the date format is yyyyMMdd then is is sortable as strings even with AM/PM
nameList.Max()
If you have a year plus hours/minutes and AM/PM then you must parse to DateTime. I recommend parsing regardless, as suggested in other answers.
// 1. Convert your string list to datetimes
IEnumerable<DateTime> dates = nameList.Select(m => DateTime.Parse(m, yourFormatProvider));
// 2. Get first and last date
DateTime maxDate = dates.Max();
DateTime minDate = dates.Min();
If I had to guess, which it seems I do. I would do this
var dates = nameList.ConvertAll(s => {
var dateString = s.SubString(6);
var timeString = s.SubString(7, 2);
var date = DateTime.Parse(dateString);
if (timeString == "PM")
{
date = date.AddHours(12);
}
return date;
});
var max = date.Max();
var min = date.Min();
Since you have specified your format as yyyyMMdd in your comment, you need to trim PM and AM from the string
List<DateTime> dateList =
nameList.Select(x =>
DateTime.ParseExact(
x.TrimEnd("PM".ToCharArray()).TrimEnd("AM".ToCharArray()),
"yyyyMMdd",
CultureInfo.InvariantCulture)
).ToList();
var Minimum = dateList.Min();
var Maximum = dateList.Max();
Console.WriteLine(Minimum.ToString());
Console.WriteLine(Maximum.ToString());
This will give you:
6/18/2012 12:00:00 AM 7/2/2012 12:00:00 AM