I am using data that includes full month names:
months <- c("March", "April", "May", "June", "July", "August", "September")
Is there a function that will convert them to numbers?
Thank you so much
You can use match() with the built-in variable month.name.
match(months, month.name)
[1] 3 4 5 6 7 8 9
Or convert your months variable to a factor and then an integer:
as.integer(factor(months, levels = month.name))
months = 1:12
names(months) = month.name
months['March']
OUTPUT
March
3
An alternative is to link month numbers and names via a named vector
months <- c("March", "April", "May", "June", "July", "August", "September")
x <- setNames(1:12, month.name)
unname(x[months] )
Related
This question already has answers here:
How do I format a date with Dart?
(24 answers)
Closed 20 days ago.
How would I go about converting a datetime object (e.g. DateTime(2000, 1, 30)) to the worded date (January 30th, 2000)?
If you look at the documentation, you can see you have the following methods exposed:
Day property
Month property
Year property
To convert the month number into the name, you could just hold a mapping of month number to month name.
To handle the ending of each day (st/nd/rd/th), you could also create a helper method.
Then you can use:
DateTime date = new DateTime(2000, 1, 30);
String month = convertMonthToWord(date.month);
String wordedDate = month + date.day.toString() + date.year.toString();
Well there are a few ways to achieve this, as #tomerpacific said. Here's one of them:
String formatDate(DateTime date) {
List<String> months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
final day = date.day;
final month = months[date.month - 1];
final year = date.year;
String dayOrderThing = "th";
if (day % 10 == 1 && day != 11) {
dayOrderThing = "st";
}
if (day % 10 == 2 && day != 12) {
dayOrderThing = "nd";
}
if (day % 10 == 3 && day != 13) {
dayOrderThing = "rd";
}
return "$month $day$dayOrderThing, $year";
}
*/ write a function daysPastThisYear that takes the current date as the name of the current month and the current day, e.g., daysPastThisYear(month: "May", day: 12), and returns how many days have past since the beginning of the year. Use a "while" loop. Ignore leap years. You may user the following list and dictionary defined:
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let monthDays = ["January": 31, "February": 29, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31]
E.g.
print(daysPastThisYear(month: "January", day: 12))
// prints 12
*/
//My code so far:
func daysPastThisYear(monthNames: [String], monthDays: [String:Int]) -> Int {
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let monthDays = ["January": 31, "February": 29, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31]
var i = 0
while month != monthNames[i] {
i ++
}
totalDays = 0
while i > 0 {
totalDays = totalDays + monthDays[i - 1]
-= i
totalDays = totalDays + day
print(totalDays)
}
}
print(daysPastThisYear(month: "January", day: 12))
Im an new to coding
Tried to calculate the number of accumulated days implementing two while loops
Multiple errors when attemtong to run
Building on top of Leo Dabus's solution but using a while loop as requested
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let monthDays = ["January": 31, "February": 29, "March": 31, "April": 30, "May": 31, "June": 30, "July": 31, "August": 31, "September": 30, "October": 31, "November": 30, "December": 31]
func daysPastThisYear(month: String, day: Int) -> Int {
var total = 0
var monthIndex = 0
// while loop though the monthNames array
while monthIndex <= (monthNames.count - 1) {
let monthName = monthNames[monthIndex]
if month == monthName {
total += day
break
} else {
total += monthDays[monthName] ?? 0
}
monthIndex += 1
}
return total
}
You can iterate the month names and each iteration just check if the month name is equal to the month, if not true add the number of days of the month to the total otherwise add the day to the total and break the loop. After the loop just return the total. Something like:
func daysPastThisYear(month: String, day: Int) -> Int {
var total = 0
for monthName in monthNames {
if month == monthName {
total += day
break
} else {
total += monthDays[monthName] ?? 0
}
}
return total
}
edit/update:
Using a while loop
func daysPastThisYear(month: String, day: Int) -> Int {
var total = 0
var index = 0
while monthNames.indices.contains(index) && month != monthNames[index] {
total += monthDays[monthNames[index]] ?? 0
index += 1
}
return total + day
}
In my app, I am using the Datetime Picker Timeline view of DatePicker, but I want to display the name of the current month. Is there a way to display month name?
As far as I can tell you can only get the number. What I usually do is:
const List months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
You can then use the number given by DateTime as the index like:
months[(DateTime.now().month)-1]
-1 because index starts at 0 and month at 1.
I need to calculate the date between two dates, and tell me how many day are in between, if more than 30 days then I will target something.
in this script D is the date (in the past) that I want to calculate from today
set X to MYdatefromSafari -- "August 26th, 2016"
set D to ConvertDate(X)
log D
on ConvertDate(X) -- sub routine to convert string "english_month dayth/st, year" to real date
set MS to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
set LW to every word of X
if (count of LW) is not 3 then return "" -- invalid format
set MI to 0 -- check month : should be in the list
repeat with I from 1 to 12
if item I of MS is item 1 of LW then set MI to I
end repeat
if MI is 0 then return "" -- the fisrt word is not in the list of months
try -- check day : it should be NNth of NNst
set DI to (text 1 thru -3 of item 2 of LW) as integer
end try
if not ((DI > 0) and (DI < 31)) then return "" -- invalid day
try -- check year
set YI to (item 3 of LW) as integer
end try
if not ((YI > 0) and (YI < 9999)) then return "" -- invalid year
return date ((DI & "/" & MI & "/" & YI) as string)
end ConvertDate
In the best scenario, that would calculate the number of date in between if less than a year, and month or year if more
EDIT :
set X to "August 26th, 2016"
set MyDate to ConvertDate(X)
set D to ConvertDate(X)
log D
set SecondDate to (current date) -- = system date
set ListDiff to DateDiff(D, CD) -- returns {diff days, diff months, diff years}
log "Days = " & item 1 of ListDiff
log "Months = " & item 2 of ListDiff
log "Years = " & item 3 of ListDiff
on DateDiff(D1, D2) -- return list with difference in days, in months, in years
-- depending if differences is less than month, or less than year or higher than a year
if D1 > D2 then -- set DStart as oldest date
copy {D1, D2} to {Dend, DStart}
else
copy {D1, D2} to {DStart, Dend}
end if
return {(Dend - DStart) div days, (Dend - DStart) div (30 * days), (Dend - DStart) div (365 * days)}
end DateDiff
on ConvertDate(X) -- sub routine to convert string "english_month dayth/st, year" to real date
set MS to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
set LW to every word of X
if (count of LW) is not 3 then return "" -- invalid format
set MI to 0 -- check month : should be in the list
repeat with I from 1 to 12
if item I of MS is item 1 of LW then set MI to I
end repeat
if MI is 0 then return "" -- the fisrt word is not in the list of months
try -- check day : it should be NNth of NNst
set DI to (text 1 thru -3 of item 2 of LW) as integer
end try
if not ((DI > 0) and (DI < 31)) then return "" -- invalid day
try -- check year
set YI to (item 3 of LW) as integer
end try
if not ((YI > 0) and (YI < 9999)) then return "" -- invalid year
return date ((DI & "/" & MI & "/" & YI) as string)
end ConvertDate
Sub-routine "DateDiff" bellow gives back a list of 3 difference values : in days, months and years.
Set X to MyDatefrom Safari
Set MyDate to ConvertDate(X)
set SecondDate to (current date) -- = system date
set ListDiff to DateDiff(D, CD) -- returns {diff days, diff months, diff years}
log "Days = " & item 1 of ListDiff
log "Months = " & item 2 of ListDiff
log "Years = " & item 3 of ListDiff
on DateDiff(D1, D2) -- return list with difference in days, in months, in years
-- depending if differences is less than month, or less than year or higher than a year
if D1 > D2 then -- set DStart as oldest date
copy {D1, D2} to {Dend, DStart}
else
copy {D1, D2} to {DStart, Dend}
end if
return {(Dend - DStart) div days, (Dend - DStart) div (30 * days), (Dend - DStart) div (365 * days)}
end DateDiff
on ConvertDate(X) -- copy your existing sub-routine
end ConvertDate
For instance if MyDate = Jan 20th 2016 and we are August 26th 2016, it will return {219, 7, 0} because différence is 216 days or 7 months (Jan to August) or 0 year (2016 both dates !).
I have a report that is pulling average results per quarter. This will be a growing, rolling report with up to 5 years of data on the graph. I need to output Q1-2014, etc. I have created the quarters
if month ({ORDER_RESULTS.RESULT_DATE}) in [1,2,3] then "Q1"
Else if month ({ORDER_RESULTS.RESULT_DATE}) in [4, 5, 6] then "Q2"
Else if month ({ORDER_RESULTS.RESULT_DATE}) in [7, 8, 9] then "Q3"
Else if month ({ORDER_RESULTS.RESULT_DATE}) in [10, 11, 12] then "Q4"
And created a formula to append the year to each quarter:
{#Quarterly} & " - " & year({ORDER_RESULTS.RESULT_DATE})
The result looks like this: Q1-2,014.00.
How do I get it to look like Q1-2014?
Thanks a bunch!
Change the formula like this.
{#Quarterly} & " - " & ToText(year({ORDER_RESULTS.RESULT_DATE}),0,"")
"Q" + ToText(DatePart("q",{ORDER_RESULTS.RESULT_DATE})),"#") + "-" + ToText(DatePart("yyyy",{ORDER_RESULTS.RESULT_DATE})),"#")