Order dates from closest to farthest from now - c#

I have a list of dates as strings in the format 'dd/mm/yyyy hh:mm:ss tt' I'm trying to order them by closest to farthest like so:
09/12/2018 12:00:00 PM
10/12/2018 12:00:00 PM
11/12/2018 12:00:00 PM
My code seems to be ordering them from the 11th to the 09th which I don't want but can't seem to get right.
DateTime now = DateTime.Now;
var ordered = herds.HerdList.OrderBy(n => (now - DateTime.Parse(n.Date_Visit)).Duration());
The above code gives me:
11/12/2018 12:00:00 PM
10/12/2018 12:00:00 PM
09/12/2018 12:00:00 PM
How can I order it the other way around?
Thanks

How can I order it the other way around?
Use OrderByDescending instead of OrderBy

You can flip the order of any Orderby(x => whatever) by simply using OrderByDescending(x => whatever).

var ordered = herds.HerdList.OrderBy(n => DateTime.Parse(n.Date_Visit));

Related

How to add Rfc3339 date time string to add one second in c#

I have this date time string 2020-06-30T20:59:59.415Z. now i have to add 1 second for this date time string. i am converting this to date time this date time are convert to other format and date time are convert to other date time. so please explain hoe i can do add one second
format required yyyy-MM-dd'T'HH:mm:ssZ
Thanks,
KD
This the code and convert this to other time
var datetime = DateTime.Parse("2020-06-30T20:59:59.415Z");
Result: {7/1/2020 2:29:59 AM}
var result = datetime.AddSeconds(1);
Result: {7/1/2020 2:30:00 AM}
string dtstr = result.ToString("o");
Result: 2020-07-01T02:30:00.4150000+05:30
required result is: 2020-06-30T21:00:00.415Z

How can i catch only date from this 2020-06-19 02:40:10.000

I wrote code to get dates from database that are older than today by 5 days
but not working
Dates are stored in db as DateTime like this 2020-06-19 02:40:10.000
I appreciate your assistance thanks in advance
var ArTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Arab Standard Time");
DateTime ArTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, ArTimeZone);
DateTime Today_DateandTime = DateTime.Parse(ArTime.ToString("yyyy/MM/dd hh:mm:ss tt"));
var query3 = (from st in Db.Support_Teckets .......
where DbFunctions.DiffDays(Today_DateandTime, st.Created_Date) > 5
"Older than today by 5 days" - but you are comparing time part too. If you only need to compare date part then you can use the var DbFunctions.TruncateTime to truncate any time part.
I am guessing you do not have any error in your code but not getting the correct data. Then write the last part of the query as following:
var query3 = (from st in Db.Support_Teckets .......
where DbFunctions.DiffDays(DbFunctions.TruncateTime(Today_DateandTime),
DbFunctions.TruncateTime(st.Created_Date)) > 5

Convert string to specific date format C#

I have a problem to convert string to date format in C#.
I'm getting the data through a row of a gridview:
string shipping_date = row["Shipping_Date"].ToString();
with format: 27/08/2015 00:00:00 but want convert to the format "yyyyMMdd".
Already used the method DateTime.TryParse, but without success...
After getting the date, it is to extract to a txt file (which I have to work). I've seen some identical issues here, but none worked for me.
Someone can help me convert to "yyyyMMdd"?
Thanks
Your question is not clear, I only guess you want something like this
var input = "27/08/2015 00:00:00";
var output = DateTime.ParseExact(input, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture)
.ToString("yyyyMMdd");
You can utilize the formatting in ToString(). You would simply do ToString(yyyyMMdd). That would use your formatting. The notations are:
Month: M (1), MM (02)
Day: d (1), dd (02)
Day of Week: ddd, dddd
Year: y (5), yy (15), yyy (015), yyyy (2015)
Hour: h (1), hh (02)
Minute: m (1), mm (02)
Second: s (1), ss (02)
There's a good chance the value is already a DateTime. Therefore, you should probably be doing this:
string shipping_date = String.Empty;
if(row["Shipping_Date"] != DBNull.Value)
shipping_date = ((DateTime)row["Shipping_Date"]).ToString("yyyyMMdd");

Create a List of hours

I'm trying to create list of hours my code like
var hours = Enumerable.Range(00, 24).Select(i => i.ToString("D2")));
it generates something like this in 24 hour format
but actually i need same thing not on 24 hours but 12 like after with AM PM postfix
what is the best way to generate hour list with 00.00 AM/PM formatted
Thanks
You can create a DateTime instance then use ToString format of hh.mm tt.
var hours = Enumerable.Range(00, 24)
.Select(i => new DateTime(2000,1,1,i,0,0).ToString("hh.mm tt"));
You need to execute the query using something like ToArray() to get the result since the behavior of linq is deferred execution.
var hours = Enumerable.Range(00, 24).Select(i => (DateTime.MinValue.AddHours(i)).ToString("hh.mm tt"));
You're dealing with time, so using appropriate type, DateTime, seems better than using int:
var hours = from i in Enumerable.Range(0, 24)
let h = new DateTime(2014, 1, 1, i, 0, 0)
select h.ToString("t", CultureInfo.InvariantCulture);
var startDate = DateTime.Today;
IEnumerable<DateTime> listOfHours = Enumerable.Range(0, 24).Select(h => startDate.AddHours(h));
If you need more flexibility and have from/to for specific times that overlap midnight (and increase step to let's say every 2h) you can use something like this:
private static IEnumerable<string> GetListOfHours(DateTime start, DateTime end, int step)
{
while (start <= end)
{
yield return start.ToString("hh.mm tt");
start = start.AddHours(step);
}
}
So this:
var start = DateTime.Now.Date.AddHours(6); // 6 am
var end = DateTime.Now.AddDays(1).Date.AddHours(2); // 2 am
var step = 1;
var list = GetListOfHours(start, end, step);
Will produce this:
06.00 AM
07.00 AM
08.00 AM
09.00 AM
10.00 AM
11.00 AM
12.00 PM
01.00 PM
02.00 PM
03.00 PM
04.00 PM
05.00 PM
06.00 PM
07.00 PM
08.00 PM
09.00 PM
10.00 PM
11.00 PM
12.00 AM
01.00 AM
02.00 AM
https://dotnetfiddle.net/8LYxA6

How to set time to Zeros in DateTime?

Lets say I have the following DateTime variable
DateTime CurDate = '26/3/2014 12:00:00 AM';
I'm wondering how can I set the CurDate so that the value will become 26/3/2014 00:00:00 AM
Note that I still want the time, but with all zeros.
**P/S: The reason for having all zeros is because the datetime value stored in SQL Server is 26/3/2014 00:00:00.000. I need to cast CurDate to have all zeros in order to match database data
You can simply use
CurDate.Date and that will give you '26/3/2012 00:00:00'
Try to format DateTime:
DateTime dt = new DateTime(2014, 06, 21, 0, 0, 0);
Console.WriteLine(dt.ToString("dd.MM.yyyy HH:mm:ss tt"));
Result:
21.06.2014 00:00:00
More informations:
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
You could try this one:
// Parse the string you have, to create a datetime.
DateTime CurDate = DateTime.ParseExact('26/3/2014 12:00:00 AM',
"dd-MM-yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
// Create the datetime you want based on the CurDate
DateTime result = new DateTime(CurDate.Year, CurDate.Month, CurDate.Day, 0, 0, 0);
For more information about ParseExact please have a look here.
Nowhere, in SQL Server or in .NET dates hasn't any presentation. They are just an numeric value. Don't care about that, both the SQL Server and .NET so smart that can pass parameters without any confusion. Just pass parameters of the correct data type.
Use 24 Hour Format
DateTime CurDate = DateTime.Parse("3/26/2014 12:00:00 AM");
Console.WriteLine("12 Hour Format: " + CurDate.ToString("dd-MM-yyyy hh:mm:ss"));
Console.WriteLine("24 Hour Format: " + CurDate.ToString("dd-MM-yyyy HH:mm:ss"));
I know it's late but I think this was the proper answer for future references:
Console.WriteLine("Current Date with 0s on time part: " + DateTime.Now.ToString("yyyy-MM-dd 00:00:00.000"));
this.dtGelisSaati = DateTime.Now;
set curency time values
this.dtGelisSaati = DateTime.Today;
this ok. zero time values set.
Put .Date and get the date part with zero time part.
I use Linq as:
var list = Results.Where(x => x.ExpDate.Date >= From.Date && x.ExpDate.Date <= To.Date).ToList();

Categories