The work I'm doing consists on finding out one's astral sign, so I put a dateTimePicker to select the date of birth, but now I'm not sure how I will get the data from that dateTimePicker to check which sign is it...
Like for example, from january first to february 14th you are this sign, from this to that you're... you know what I mean.
You are not concerned about the year. You just need the month and day values:
DateTimePicker dtp = new DateTimePicker();
int month = dtp.Value.Month;
int day = dtp.Value.Day;
Then, you can do if-else statements around the various astral signs:
if (month >= 3 && day >= 20 && month < 4 && day <21 )
{
return AstralSign.Aries;
}
else if (/* some other sign */)
{
// ...
}
You can create an enum for the signs as well:
enum AstralSign
{
...
Aries
...
}
There is no built-in functionality in DateTimePicker for achieving this. You will have to either go for if-else approach or a switch statement to find out which sign a particular date belongs to..
If your DateTimePicker was called dateTimePicker, you could do it like this:
DateTime pickedDate = new DateTime(dateTimePicker.Value.Year, dateTimePicker.Value.Month, dateTimePicker.Value.Day);
And then you could use pickedDate as you want.
There is nothing in built in for Zodiac signs , so i would recommend you building your own list for the Zodiac signs.
Comparing a date falls in which zodia part you can go along with DayOfYear part of the Datetime value selected in the DatePicker (dateTimePicker1.Value)
So 0 to 46 could be your first zodiac and compare this with the DayOfYear part and you will have your Zodiac sign (else year would be a problem while comparing DateTime objects)
The DateTimePicker has a property Value. This contains the selected date by the user.
If you like to get informed when the user made his selection, simply subscribe to the ValueChanged property.
Afterwards simply compare if the the given date falls within the range of any astral sign.
To get the astral sign i would built up a nested dictionary Dictionary<int, Dictionary<int, AstralSign>> which could then be access by
selectedSign = astralSigns[pickedDate.Month][pickedDate.Day];
Related
I have a range of 2 dates given by my business: "we would like to have all data from July and 6 previous months". I create two dates:
_range1.MaxDateTime = new DateTime(today. Year, 7, 31);
_range1.MinDateTime = rangeLastDate.AddMonths(-6).AddDays(1);
Because the system will always perform the calculation on last 6 months. Last date is also always end of month. I have no problem to get the last day of the month. C# has the feature EndOfMonth() for this. But when I do so I always get the last day at midnight. when I create the DateTime() myself I also get a date a midnight.
In my code I use Entity Framework with LinQ. (old EF, not Core yet)
_range1_ShippedQuantities = DbContext.Job_Dtl
.Where(j=> j.LastShipDate >= _range1.MinDateTime && j.LastShipDate <= _range1.MaxDateTime)
This is not correct because my last datetime is the 31 at midnight and not the first of next month at midnight. I should do
_range1_ShippedQuantities = DbContext.Job_Dtl
.Where(j=> j.LastShipDate.Date >= _range1.MinDateTime.Date && j.LastShipDate.Date <= _range1.MaxDateTime.Date)
Or I can also manipulate the dates do force the time at 23:59:59... what I don't like. I still miss 1 second. I can add 999 milliseconds but i will still tell you I miss one millisecond. I can also add one day in a temp variable and use this temp variable in my LinQ query... yes but I have the feeling there is something better. How do you deal with these ranges?
But LinQ does not allow this. LinQ for entities cannot recognize the date.
As several people have commented on your question, the simplest and most common way to solve this problem is to choose an exclusive end date rather than an inclusive one.
var minDateInclusive = rangeLastDate.AddMonths(-6).AddDays(1);
var maxDateExclusive = rangeLastDate.AddDays(1);
DbContext.Job_Dtl
.Where(j=> j.LastShipDate >= minDateInclusive
&& j.LastShipDate < maxDateExclusive);
I need to pick a valid date from a datepicker in my automated tests. I'm very new to C#, Selenium and Visual Studio. My current code is:
DriverGC.FindElement(By.CssSelector(".formElement.jsDatePicker")).Click();
DriverGC.FindElement(By.CssSelector(".datePicker_list li[data-day='25']")).Click();
This works ok, as long as 25 is valid for example. If I run the test say on the 26th of the month, the next available 25 is on the next month, so not valid and my test fails.
Can I put a line of code in that will use my system date, and then say +2, and then select this as the date in the datepicker, so no matter when I run the test during the month, it will always pick a valid date?
When I understand it correctly you want to select any valid date in your datepicker, right?
I would suggest creating an Array with all valid dates first, and than randomly select one of those. This is Java code, but the C# syntax should be similar.
//create list of all valid elements
List<WebElement> elements = driver.findElements(By.cssSelector("selectorForValidDates"));
//get a random index of the list
int i = new Random().nextInt(elements.size());
//click on valid element
elements.get(i).click();
It's important to have proper attributes on your elements to easily select them. Like class="validDate" on all dates that can be selected. This way your css-selector could look like .datePicker_list li.validDate which would only return the elements you can actually use.
You can use this syntax:
int currentdayOfTheMonth = DateTime.Now.Day;
string cssLocator = string.Format(".datePicker_list li[data-day='{0}']", currentdayOfTheMonth);
DriverGC.FindElement(By.CssSelector(cssLocator)).Click();
This code will get first the current day of the month, build the css locator and then find the element. If you want to add one day or subtract you can use the AddDays method of the DateTime.
int currentdayOfTheMonth = DateTime.Now.AddDays(1).Day;
So I'm working on a calendar-based application in C#, and I need to get the days of the week that an event is to repeat itself (for instance, every Monday/Friday).
I'm storing the events in XML, defined similarly as such:
<events>
<event startDate = "Insert Date Here" endDate = "Insert Date Here">Event Name</event>
</events>
How could I simply and reliably store and retrieve specific days of the week in this XML file?
If it helps, I'll be specifying the days of the week by a CheckBoxList, which I will be looping through to check for, well, checks.
You can use the following from this XML library
XElement a = new XElement("root");
a.Set("day", DayOfWeek.Thursday, true); // true = set as attribute of 'a'
DayOfWeek day = a.Get("day", DayOfWeek.Friday); // DayOfWeek.Friday is default
Console.WriteLine(day);
Output:
Thursday
Generated XML:
<root day="Thursday" />
It would be easiest to serialize the values as ints then use them with an enum in the C# code (0 = Sunday, 1 = Monday, ect). You could also use the words.
Then you can read them and use the built in enum values as listed in the docs in code to maintain readability. Which, by the way,
if cast to an integer, its value ranges from zero (which indicates
DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).
If you provide specifics on how you are reading the data from the xml into your C# app I can provide more specifics.
This seems like you're injecting fragility into your design. Suppose that your <event>'s day-of-week (DoW) and date disagree? What does your app do?
I think it's better to calculate the day of week based on that initial date:
var repeatingEvents = new List<DateTime>();
repeatingEvents.Add(new DateTime(2015, 4, 15));
for (var i = 1; i < 5; i++)
{
repeatingEvents.Add(repeatingEvents[0].AddDays(7*i));
}
This gives you 5 events on the same day of week without having to worry about what the actual DoW is.
If you're absolutely insistent on serializing DoW, look to DateTime: It already supports a DayOfWeek function, and has a DayOfWeek enum for your use.
We've been trying to figure out how to compare two calendar dates in C# ASP.NET.
The problem we have is that when a customer uses the calendar, they can choose to set it back in time.
Example:
2015-09-23 to 2015-08-23 <- this is something we don't want, it is set back in time.
How do we check if the start date is later than the end date.
We have seen someone using CDate? Which we did not understand how to use this.
We have tried to just remove the "-" from a string, convert it to an int and then calculate if the answer is less than 0.
But this was a big workaround. Is there an official or better way to do this?
If any more information is needed, feel free to ask!
You can use the CompareValidator in ASP.Net
<asp:CompareValidator ID="myID" ControlToCompare="StartDate"
ControlToValidate="EndDate" Type="Date" Operator="GreaterThanEqual"
ErrorMessage="The dates are not correct." runat="server"></asp:CompareValidator>
or else
DateTime d1 = new DateTime(2015, 9, 1);
DateTime d2 = new DateTime(2015, 8, 2);
if (d1 < d2)
{
//error
}
Easy enough. I assume its only the date part you are interested in and not the time. The DateTime.CompareTo() method compares two dates and returns one of the following integer values:
-1 Less Than
0 Equal To
1 Greater Than
Using this and the Current Date we can easily tell if the given date is in the past. This method will take a date string, like the one provide in your example, and compare it to the current date (ignoring the time element) and return true if the given date is in the past, or false if it is equal to or greater than the current date.
private bool IsDateInThePast(string inputDate)
{
DateTime dt = DateTime.Parse(inputDate);
return dt.CompareTo(DateTime.Parse(DateTime.Now.ToShortDateString())) < 0;
}
Obviously, you might want to add some validation and/or exception handling to handle an invalid input string.
I have a function which convert date to my formatdate and compare it to CurrentDate.
It worked well when I test it with date is a date in Octorber compare to today (16/10/2013)
But if date is a date in November, the value return is true which means date < currentdate. I dont know why, anyone can explain it to me ? Thanks so much.
function IsPast(date) {
var fullDate = new Date();
var twoDigitMonth = fullDate.getMonth() + 1 + "";
if (twoDigitMonth.length == 1) twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1) twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
var startDate = $.fullCalendar.formatDate(date, 'dd/MM/yyyy');
if (startDate <= currentDate) {return true;}
return false;
}
If you are being passed a Date object (and that seems to be what you get), then you can simply do:
function isPast(date) {
return new Date() > date;
}
If date isn't a date object, convert it to one first.
Note that identifiers starting with a capital letter are, by convention, reserved for constructors.
Edit: When I first read your question, I saw that you were using day first dates and jumped to my conclusion without really taking in your code. I've taken a closer look, and it appears my initial conclusion is just a red herring. The real problem is that you are taking two dates, converting them both to strings, and then trying to compare them. That's just silly - it will only tell you which string comes first alphabetically. Even sillier, is that for one of the dates you manually construct the string, but for the other one, you use a date formatting function. Why wouldn't you just use the date formatting function for both dates?
RobG's answer is correct and should be accepted.
When I see the date "01/11/2013", I read it as January 11, 2013, whereas you read it as 1 November, 2013. See the predicament the JavaScript engine is in when it tries to parse that date?*
When parsing JavaScript dates, I like to use year first dates, as these are unambiguous as far as I know. Everybody interprets "2013/11/01" as 2013 November 1. Including every JavaScript Date implementation I've seen.
Change your date format to be unambiguous.
*Frankly, I'm surprised you are seeing that behavior. I would expect that your system would be configured to expect day first dates and that the JavaScript engine would use those settings to properly parse the date. But, I'm not that surprised.