Interpolating an argument into a variable name - ruby

I have several pages where a date is input. I want to capture the day, month and year and store them to be checked later on. Each page has its own unique ID but uses the same IDs for each field: day, month and year.
I've got this on each page where the date is captured at the moment:
#page_10_day = find(:id, 'day').value
#page_10_month = find(:id, 'month').value
#page_10_year = find(:id, 'year').value
But I'd like to do something like this:
capture_date("page_10")
def capture_date(page)
#"#{page}_day" = find(:id, 'day').value
#"#{page}_month" = find(:id, 'month').value
#"#{page}_year" = find(:id, 'year').value
end
Is there a way of just having one method to capture the date and naming objects using an argument?

Related

How pass month value and get start and end date of month In laravel

I am a new Laravel and using Laravel 5.5.
I want to pass value of month and get start and end date of passing month.
Suppose pass 5 (May month) the get value
Start date: 01-05-2019
End date: 30-05-2019
I have tried:
$finalMonth = 5;
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
$endMonth = Carbon::now()->addMonth($finalMonth)->endOfMonth()->format("Y-m-d");
But It's given the wrong result because of it adds current month+pass month.
Means 4+5=9 that means sept.
Please Help
Instead of using:
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
Use instead the month method:
$startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");
See: carbon-get-month-by-number-returns-march-for-number-2
Use instead Below :
$startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");

How to get only first part of the date ('DD') from an entire date?

I've converted a date into string. Now, I want to parse a string to get the DD part from DD-MM-YYYY.
For e.g.
If the date is 03-05-2017 (DD-MM-YYYY) then the goal is to get only first part of the string i.e. 03 (DD).
You've tagged this question as a ServiceNow question, so I assume you're using the ServiceNow GlideDateTime Class to derive the date as a string. If that is correct, did you know that you can actually derive the day of the month directly from the GlideDateTime object? You can use the getDayOfMonth(), getDayOfMonthLocalTime(), or getDayOfMonthUTC().
You could of course, also use String.prototype.indxOf() to get the first hyphen's location, and then return everything up to that location using String.prototype.slice().
Or, if you're certain that the day of the month in the string will contain an initial zero, you can simply .slice() out a new string from index 0 through index 2.
var date = '03-05-2017';
var newDate = date.slice(0, 2);
console.log(newDate); //==>Prints "03".
var alternateNewDate = date.slice(0, date.indexOf('-'));
console.log(alternateNewDate); //==>Prints "03".

How to find the date number given the Year, month, day of the week and the week it falls on in ruby

So i'm new to ruby and not to familiar with the syntax of the Date gem, but is there a way to find the date of a moving holiday like fathers day?
So my current class for the moving holidays looks like this:
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, day)
end
end
and my input looks like this:
fathers_day = MovingHoliday.new("Fathers Day", 6, "sunday", 4, year)
The 6 is the month, "sunday" is well the day_of_the_week, and 4 is the week it falls on.
I just can't figure out the syntax if there is any that would/could do conversion for this.
And I can't use any gems other then rubys Date.
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, 1)
#my_date += 1 while !my_date.send("#{day_of_week}?")
#my_date += (7 * (week - 1))
end
end
Not pretty, but it works. It takes the first of the month, moves up a day until it's the correct day of the week, and then increases by the appropriate number of weeks.
As it stands, it assumes that you're given an actual day of the week. You'll probably want to sanitize the input in some manner or convert from "sunday" to 0 (my_date.wday returns 0 for sunday, 1 for monday, etc)
date in Ruby is not a gem, it's part of the standard library, it's just not loaded unless you require it. (In Ruby, require is used for both purposes, loading gems and loading parts of the standard library that are not loaded by default.)
I don't know of any built-in functionality that would calculate the date for you, I think you'd have to write that yourself, since you don't want to consider other gems.

django-queryset: query many columns at once

I have a django app (w/postgresql database) that stores information on nest conditions for an endangered bird. Data is collected over multiple sites with different #'s of nests at each site. The nest conditions also have a unique date range per site.
DB Columns: site_name, date, nest_01, nest_02, nest_03 ... all the way to nest_1350.
The nests have values of either empty, 1E, 2E, 3E, or 4E.
Is there a way to do 1 query of all (1-1350) of the nest columns looking for '1E'?
Thanks
Do you actually have a model with 1350+ columns?
If I were you I'd normalize the whole setup like this:
class Site(Model):
site_name = Charfield()
date = DateField()
class Nest(Model):
name = Charfield()
condition = Charfield()
site = ForeignKey(Site)
And then query it like this:
site = Site.objects.get(pk=1) # just a Site, I assume you know a Site
nests = Nest.objects.filter(site=site).filter(condition='1E') # your desired nests

Get the the most recent and the one before the most recent item

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Resources