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');
Related
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();
I have a large collection of text files that need to be parsed using Regexes. Some of the data that I'm collecting are dates that come in all sorts of formats, such as 12/1/15, Dec 1, 2015, 12-1-15, etc. They sometimes have a year listed and sometimes don't. My problem occurs when I have dates that span two years, i.e. 12/1 - 1/8, where the first date needs the year 2015 and the second date needs the year 2016. Currently I'm parsing them as strings and trying to convert them to DateTimes. This adds the current day's year, so if it's parsed in 2015, the second date is wrong and if it's parsed in 2016 the first date is wrong. Is there a way to determine when Convert.ToDateTime adds the year since the string was missing one? If I could determine this I have a way to determine which year needs to be added.
Convert.ToDateTime just uses DateTime.Parse. My understanding is that when interpreting MM/dd formats, it will always assume the current year.
In your scenario, it sounds like you will need to make some determination on how you want to handle this. For instance, you could test that if the latter date precedes the prior date, you add a year.
I need to read a String in the following format: "6102015" (meaning October 6th, 2015) and turn it into DateTime objects.
I tried the following code, which did not work:
DateTime.ParseExact("6102015", "dMyyyy", CultureInfo.CurrentCulture);
But when I tested the code using the date string with an extra 0, it worked.
DateTime.ParseExact("06102015", "dMyyyy", CultureInfo.CurrentCulture); // works correctly
Is there a way to read this date format without having to add the 0?
I thank you in advance for any help.
Is there a way to read this date format without having to add the 0?
Adding a 0 is the least of your worries, IMO. That takes one line of code.
Assuming you've got a copy of the database or something you can alter, effectively, I would:
Create a field of a date/time type, or if you must use a string, do so but use an ISO-8601 format (yyyy-MM-dd)
Parse all the values which are already 8 characters
Parse all the values which are 6 characters by inserting two 0s (so abcccc becomes 0a0bcccc)
For each remaining value, of the form abcyyyy:
Try parsing it as 0abcyyyy
Try parsing it as ab0cyyyy
If only one parse worked, store that result in the new column
Now look at all the remaining rows (i.e. the ones you haven't populated with a "known good" value
You may be able to use other data (such as insertion order) to work out which is the "right" parse...
You may not - in which case you need to decide what to do
I have one string such as Date which has the value of Dates. Below is a sample value:
string Date = 05;
When Date is between 01 and 09, Date value should ignore "0". For example it should be "5".
If it is "20" means then it should not ignore "0".
How to do this in C#?
Seems like you're trying to convert a string to an integer ?
Try Convert.toInt32(date); which will return an int... then calling ToString() will give you the string representation if necessary.
You can use String.Replace() or Regex.Replace(). But it's hard to understand how to solve your problem effectively without more details/code.
you know you could use an int for this...
int month = 5; //is 5 and not 05 as an example
why do you have a string?
Also, in c# Dates have a special variable, the DateTime. You can store months, days, years, hours etc. in them. This might be related to what you are looking for.
I have a string with a value in the format of
dd/mm/yyyy
Now I want to compare that to another string and check if they are equal, the other string can have the value of
dd/mm/yyyy
or sometimes when the day is between 1 and 9:
d/mm/yyyy
Sometimes when the month is between 1 and 9:
dd/m/yyyy
So there are a couple of instances where the string isn't equal but the actual date is.
First I only saw that in some cases when the day is between 1-9 it doesn't start with a zero so I did:
createdDate = dateField.value.ToString().Substring(0, 10);
createdDate = createdDate.Trim();
if (createdDate.Length == 9)
createdDate = "0" + createdDate;
but then I saw that it can be the same for month so there must be a better way to check this?
Why not just parse both, one with the format string "dd/MM/yyyy" and one with the format string "d/M/yyyy" and compare the returned DateTime values? After all, the date being represented is the important part, not the string representation, presumably.
You should use DateTime.Parse to convert both values to dates and then compare the dates.
DateTime.Parse("01/01/2001") == DateTime.Parse("1/1/2001")
When it are strings, that represent dates, why don't you try to parse them to a date, and compare the date instances that you become ?
you just have to make sure that you use the correct pattern when parsing.
Check out the DateTime.TryParse method.
I'd parse both dates into DateTimes and then compare them, that way you don't have to worry about different formats in the compare.
First, parse the strings into values of type DateTime (DateTime.ParseExact), then use the standard equality comparer (==) to compare the dates.