Loop over hour:minute [duplicate] - ruby-on-rails

This question already has answers here:
How can i loop through a daterange with different intervals?
(5 answers)
Closed 8 years ago.
I'm with Rails 4:
I need to make a loop in a table to display time of the day as title of column, like that
8:00 | 8:30 | 9:00 .... 20:00 | 20:30 | 21:00
Do you have any idea how to define the loop?
I've try complicated things with step() but meaby I have miss a easy way using Time?
Thanks

You could use Rails DateTime like this (for n steps you want)
startdate = DateTime.new(2001,2,3)
interval = 30
formatstr = '%H:%M'
(0..n).map{|offset| startdate + (offset * interval).minutes }
.map{|date| date.strftime(formatstr)}

Related

IIf Statement Looking at Months

I would like to create an IIf statement to calculate when the expected leaving date of a child is.
For example, a child who is born before 31/08 is expected to leave nursery after 4 years, a child who is born after that date is expected to leave after 5 years.
Now what I was trying to do is ask an IIF statement which looks at the date of birth and decides whether to calculate for 4 years or for 5 years. However I keep running into issues with the code that I am using which is
= IIf([Date of Birth]>#31/08/0000# , =DateAdd("yyyy",4,[Date of Birth]) , =DateAdd("yyyy",5,[Date of Birth]))
as there are multiple children with different dates of birth. There needs to be a way to look specifically at the months only.
EDIT:
Turns out that is not what my boss needs, what he needs is basically to display when the child is leaving from the nursery i.e. when the new school term rolls around and the child is 4 years old. if the child is born before September he is applicable to start school that year. if he isn't the child is applicable to start school the next year on the month of September.
And right now I have no idea what to do as my attempts of doing an IIF function have completely failed. Can anyone Help?
Try with:
=DateAdd("yyyy", IIf([Date of Birth] > DateSerial(Year([Date of Birth]), 8, 31), 4, 5), [Date of Birth])
Edit 1:
You can use DateAdd like:
=IIf(DateAdd("yyyy", 4, [Date of Birth]) < DateSerial(Year(Date()), 9, 1), "Start school this year", "Postpone school start")
Edit 2:
Or you could calculate the age of the children on September 1st:
AgeAtSeptember: Age([Date of Birth], DateSerial(Year(Date()), 9, 1))
using this function:
' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
' leap years
' dates of 29. February
' date/time values with embedded time values
' any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
ByVal DateOfBirth As Date, _
Optional ByVal AnotherDate As Variant) _
As Integer
Dim ThisDate As Date
Dim Years As Integer
If IsDateExt(AnotherDate) Then
ThisDate = CDate(AnotherDate)
Else
ThisDate = Date
End If
' Find difference in calendar years.
Years = DateDiff("yyyy", DateOfBirth, ThisDate)
If Years > 0 Then
' Decrease by 1 if current date is earlier than birthday of current year
' using DateDiff to ignore a time portion of DateOfBirth.
If DateDiff("d", ThisDate, DateAdd(IntervalSetting(DtInterval.dtYear), Years, DateOfBirth)) > 0 Then
Years = Years - 1
End If
ElseIf Years < 0 Then
Years = 0
End If
Age = Years
End Function

Formatting a date - Rails [duplicate]

This question already has answers here:
Rails formatting date
(4 answers)
Closed 4 years ago.
I have a variable containing a date in this form: (2018-03-21 18:49:49 UTC)
and I would like to display in this form: (day / month / year) but I can not find a solution. Do you have an idea ? Thank you !
Well, I guess this could work.
print "What year?"
year = gets.chomp!
print "What number month?"
month = gets.chomp!
print "What number day?"
day = gets.chomp!
puts "#{day}/#{month}/#{year}"
I'm new to ruby but I think this should work.

Swift Date object with different format [duplicate]

This question already has answers here:
Comparing NSDates without time component
(17 answers)
Closed 5 years ago.
I've got a Date object that holds year/month/day and hour/minute/second data. I need to drop the hour/minute/second part as it's making comparing days problematic.
The issue is that the only obvious way I see of doing it is turning the day into a String and then using a DateFormatter with yyyy/mm/dd format to turn it BACK into a Date. This seems like a waste, is it really the only way?
Thanks a lot.
Never convert dates to String and back to Date.
You are looking for the startOfDay function of Calendar:
let date = Date()
let startOfDay = Calendar.current.startOfDay(for: date)

How do I get a label in Xcode to show the day of the week (Swift/Swift 2) [duplicate]

This question already has answers here:
Displaying the Day Of The Week From Date Picker
(2 answers)
Closed 7 years ago.
I'm an absolutely coding newb and I want to create a school planner app. I have the general nuts and bolts sorted - but I want to create a function where a label displays the day of the week, which a button would then interpret and would then send the user to the appropriate View Controller with that day's timetable in.
I've tried YouTube and of course here, and I can make no sense of it at all. Can someone treat me like a little baby and explain it to me. The name of the label is DayLabel1 and I think I can connect it as an IBOutlet.
I make no sense out of this whatsoever, and if you can't help me, I am either
a) doomed
b) still doomed
This is not a duplicate as I'm not wanting a date picker. I'm a newb and just want a hand.
Thank you! :-)
[[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]] weekday];
This code gives you to number of day in week which start from Sunday (index of Sunday is 0). You can make an String enum to get string using this index.
Here is the Swift:
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let dateComponent = calendar.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: date)
let weekday = dateComponent.weekday

How to correctly get the days for "posted X days ago" for a blog post date [duplicate]

This question already has answers here:
Number of days between two NSDates [duplicate]
(16 answers)
Relative string from NSDate
(5 answers)
Closed 8 years ago.
So I'm trying to get the amount of days a blog post was posted ago. The end result would be something like "posted X days ago."
After spending some time on the problem, I figured it out by getting the NSTimeInterval, dividing to get days and then rounding. Although I got the output I wanted, I feel I am doing it wrong or there is a much more straight forward way of doing it.
tempDate is a NSDate object of when the blog was posted.
NSTimeInterval timeSince = [tempDate timeIntervalSinceNow];
timeSince = timeSince/60/60/24*-1; // seconds to days
int daysSince = lroundf(timeSince);
you could use NSCalendar and NSDateComponent to get the number of days (and you don't even need to convert your tempDate)... See the good answer given here Number of days between two NSDates

Resources