Distinct and DateTime in c# - c#

I have 2 problems. I'm trying to make my first application in xamarin, and i have list of Dates. First problem is format. When i add bind datetime to label it looks like this "25.11.2021 00:00". What i can do to have only date? second problem i have with distinct. A lot of the dates are the same and I want only one unique. I can't use DistincBy, so i write something like this MyList.Select(x => x.dateTime).Distinct().ToList() but that not work. Someone can tell me what I do wrong?

Date part
Use the .Date of DateTimes values, it will give you only the Date. Another better solution is to use the function .ToString(string format) to extract the date in a string that you will be able to use in your label. (documentation about date format)
Exemple code :
string myDate = DateTime.Now.ToString("dd.MM.yyyy");
Distinct part
For the .Select() problem, also use the .Date in your filter because it will check if dates are the same and not datetime, which contains precise data allowing to have tiny differences between 2 dates.
The code for the .Select() would be :
MyList.Select(x => x.dateTime.Date).Distinct().ToList();

Related

extract date from string that contains time as well in C#

I get an string from one of DTO i.e. "2020-05-17T00:00:00" and this is automapped to one of view model. Actually i only need date part i.e. 2020-05-17 and not time part from it.
Within automapper i tried directly formatting string in following way.
String.Format("{0:yyyy-MM-dd}",)
but automapper says that this can only be done within a type and not during mapping.
Also tried below way but still failed to format string.
.ForMember(src => src.FromDate, act => act.MapFrom(dest => DateTime.ParseExact(dest.FromDate, "yyyy-MM-dd", CultureInfo.InvariantCulture)))
Not able to track where the issue is.
Any pointers or help is welcomed.
Try to get a DateTime instead of string then use the method
.ToShortDateString()
If you can't get a DateTime, convert the string to DateTime with
DateTime oDate = DateTime.Parse(iDate);
Then use the toshortdatestring

SQLite intermittently querying successfully between two dates [duplicate]

This question already has answers here:
SQLite DateTime comparison
(14 answers)
Closed 9 years ago.
Im storing my dates as string in format 'dd-mm-yyyy'. Now I want to do a comparison like this:
SELECT strftime('%d/%m/%Y', myDate) as myDate
FROM myTable
WHERE myDate>='01/07/2013' and myDate<='24/07/2013'
But I get nothing. Whats wrong with this query?.
THIS IS NOT A DUPLICATE QUESTION. I was storing everything as String, no as a DATE or DATETIME. I was trying to do a comparison between strings with date format. So is not fair the down vote.
There is no way you can get your query to work like that with your current setup:
I'm storing the date as a String so I guess with the using of strftime I can get what I want. Or am I wrong?
Basically: You're wrong!
The problem here is that a string formatted like that will not be sortable in the way you want.
Specifically:
myDate>='01/07/2013' and myDate<='24/07/2013'
Here, any date that is between the first and the 24th of any month in any year will match this. Ie. this will match: "02/01/1900", as will this: "02/12/2099".
The reason for this is that string comparisons are alphanumerical, not numerical (or datewise). "02" is greater than "01" and less than "24", and the rest is just redundant.
The "logical" way to format a date as a string is to start with the most significant value and work your way downwards, ie. this format: "yyyy/mm/dd", and not the other way around, but the real logical way to store a date, is to store it as a date, and not as a string.
The only way to work with the strings is to convert each string to a date and work with that, but the best way is to fix your schema.
If you absolutely cannot change your current schema, here is a way to make it work, but I do not advice this:
where (substr(myDate, 7, 4) || '-' || substr(myDate, 4, 2) || '-' || substr(myDate, 1, 2)) between '2013-07-01' and '2013-07-24'
This will pick apart the strings, put them together again in the right order, before doing the comparison. Also note the format of the last two dates there.
Did you try this :
SELECT strftime('%d/%m/%Y', myDate) as myDate
FROM myTable
WHERE myDate between date('01/07/2013') and date('24/07/2013');

Date type in MySql

I'm new to MySQL and C#.
I stored certain values in a column with data type Date. I did not want the time, only the date to be stored.
On viewing these values using phpMyAdmin or MySql command line, I see them in the format:
YYYY-MM-DD
However, when I retrieve these values in to my web application, they are displayed in the following format:
YYYY-MM-DD HH:MM (the time is specifically 12:00).
Why does this happen? And how can I prevent this from happening?
when you store in C# your date field, you use DateTime object. In this object when you don't specify the time part will be put a default value depends on Globalization.
You can study how DateTime works here
You can convert the date to the format you like when you fetch the data, using date_format():
select date_format(datecol, '%Y-%m-%d')
This returns the value as a string.
You shouldn't retrieve the value as a string from mysql. Why? Because if you ever need to do any operations on that value, such as adding a day, then you will need to parse it back into a DateTime again. String parsing can be slow, and when it comes to dates they are prone to errors like misinterpretation of mm/dd/yyyy and dd/mm/yyyy formatting.
The problem you have is that .NET does not have just a Date type. It only has a DateTime type. So loading a MySQL DATE type, is going to get a DateTime with the time portion set to midnight.
There's no direct problem with that, except on how are outputting the result. If you just call .ToString() without any parameters, or you implicitly use it as a string, then you are going to get a result with the full date and time. You simply need to provide a parameter to indicate what formatting you want.
Without any parameters, you are getting the General "G" format. This is explained in the documentation here.
In other words:
yourDateTime.ToString() == yourDateTime.ToString("G")
You can read about all of the other formats available, here and here.
In particular, if you just want the date, then you probably want to do this:
yourDateTime.ToString("d")
Based on your comments, you should be doing this instead:
MySQL Query:
SELECT Gbstartdate FROM TblGbDef
C#:
DateTime gb_start_date = (DateTime) datareader[0];

DatePicker/TimePicker format in WP7

I have nice UI with DatePicker and TimePicker but I need to change the final look of variables which they give.
Date looks like that: 28/02/2012
Time looks like that: 09:18
I would like to have it in SQL DateTime format, so I have two questions:
1) How to change separators in date?
2) How to convert it into SQL format: 2012-02-28 09:18
The only idea what I have in my mind now is to take each part (year, month..etc.) and manually make new datetime variable. Also here I have question. If I will have good format, can I send it to database as a string?
You can set the System.Threading.Thread.CurrentUICulture and/or System.Threading.Thread.CurrentCulture to a custom Culture instance that returns the date & time in the format you require!

Storing a short date in a DateTime object

I'm trying to store a shortened date (mm/dd/yyyy) into a DateTime object. The following code below is what I am currently trying to do; this includes the time (12:00:00 AM) which I do not want :(
DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());
Result will be 10/19/2009 12:00:00 AM
DateTime is an integer interpreted to represent both parts of DateTime (ie: date and time). You will always have both date and time in DateTime. Sorry, there's nothing you can do about it.
You can use .Date to get the date part. In these cases, the time will always be 12:00 but you can just ignore that part if you don't want it.
You only have two options in this situation.
1) Ignore the time part of the value.
2) Create a wrapper class.
Personally, I am inclined to use option 1.
A DateTime will always have a time component - even if it is 12:00:00 AM. You just need to format the DateTime when you display it (e.g. goodDateHolder.ToShortDateString()).
Instead of .Now you can use .Today which will not remove the time part, but will only fill the date part and leave time to the default value.
Later on, as others pointed out, you should try to get the date part ignoring the time part, depending on the situation.
You'll always get the time portion in a DateTime type.
DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());
will give you today's date but will always show the time to be midnight.
If you're worried about formatting then you would try something like this
goodDateHolder.ToString("mm/dd/yyyy")
to get the date in the format that you want.
This is a good resource msdn-dateformat
You can also check out Noda Time based off the Java Joda Time library.
DateTime object stores both the date and the time. To display only the date, you would use the DateTime.ToString(string) method.
DateTime goodDateHolder = DateTime.Now;
// outputs 10/19/2009
Console.WriteLine(goodDateHolder.ToString("MM/dd/yyyy"));
For more information on the ToString method, follow this link
You might not be able to get it as a DateTime object...but when you want to display it you can format it in the way you want by doing something like.
myDateTime.ToString("M/d/yyyy") which gives 10/19/2009 for your example.
DateTime is merely a UInt64 with useful and clever formatting wrapped around it to make it appear like a date plus a time. You cannot eliminate the time element.

Categories