Min Date and Max date - c#

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

Related

Compare dateTime "MM/dd/yyyy hh:mm:ss" with "yyyy-MM-dd hh:mm:ss" in c# LINQ

How to check dateTime greater than with below format date Time.
In "CreatedDate" column have below format values.
Created Date
2020-03-01 10:59:50.250
2020-03-01 10:40:39.610
2020-03-01 10:39:18.087
2020-02-29 07:39:18.087
public IQueryable<UserProfile> GetTopUserProfiles(System.DateTime startDateTime)
{
return GetDbContext(toUpdate).Get<UserProfile>( w =>w.CreatedDate >= startDateTime);
}
var dtstart = DateTime.Now.ToString("yyyy-MM-dd");
statDateTime = Convert.ToDateTime(dtstart).AddHours(8); //add 8 hours to current date.
//start DateTime value: 3/1/2020 8:00:00 AM
var count = _userProfileRepository.Value.GetTopUserProfiles(startDate).Count();
count is returning 0.
Expected count is 3
Basically, You no need to convert to yyyy-MM-dd.
You should consider whether the server time value between .NET back-end and SQL Database is the same or not.
Try below code
return GetDbContext(toUpdate).Get<UserProfile>( w => DateTime.Compare(w.CreatedDate,startDateTime) > 0);

get date without time in c#

i have a column on my table "AbsenteeismDate" (type=date) when i want to get rows contain a date it return 0 row.
this is my C# code :
DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString());
var Abs = dbs.GETAbsenteeisms.Where(a => a.AbsenteeismDate == ClassDate ).ToList();
i checked it there is a problem :
"AbsenteeismDate" on database and "ClassDate" aren't equal.
eg.
AbsenteeismDate=1396-05-31
and
ClassDate=1396-05-31 12:00:00 AM
how can i get Date without Time with DateTime type because AbsenteeismDate's type is date on my database.
sorry i can't speak English very well.
A DateTime always has a date and time portion, but if you want to get a DateTime of that date and the time value set to 12:00:00 midnight (00:00:00) use DateTime.Date:
var Abs = dbs.GETAbsenteeisms
.Where(a => a.AbsenteeismDate == ClassDate.Date)
.ToList();
You do just define date time but without the time:
string date = System.DateTime.Now.ToString("yyyy-MM-dd");
Use the Date property:
DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString());
var date = ClassDate.Date;
Otherwise you will have to convert to string as follow
var date=ClassDate.ToString("yyyy-MM-dd");
This works for me:
string a = DateTime.Now.ToShortDateString();
ToShortDateString() converts the value of the current System.DateTime object to equivalent short date string representation. This means the Console output woulf be e.g. 01.01.2017
You can try like this byt setting the date format:
DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString(("yyyy-MM-dd"));
var Abs = dbs.GETAbsenteeisms.Where(a => a.AbsenteeismDate == ClassDate ).ToList();

To Get Date From DateTime Variable

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.

how to remove time part

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

asp.net DateTime manipulation

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);

Categories

Resources