Flutter - Display just month in Datetime picker Timeline - flutter

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.

Related

DateTime object to worded date [duplicate]

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";
}

fix code implement while loop with dictionary in Swift code

*/ 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
}

Get the Month and Year X number of months from now

I am attempting to generate a PageView that will display the Month and Year related to the number of times you swipe in either directon.
Example 1:
I swipe right twice, so I get Feb 2021
Example 2:
I swipe left 12 times, so I get April 2020
I have attempted to create a DateTime.now() and subtract an integer of months, but I'm not having much luck. I have looked at various plugins like DateUtils, but again no luck.
I have been at what should be a simple solution for while now and would appreciate a guidance.
The closet I get is the following which requires me to know the days in each month which isn't ideal
(DateTime.now().subtract(Duration(days: 90)).toString())
From DataTime docunamtion:
Returns a new [DateTime] instance with [duration] added to [this].
var today = DateTime.now();
var fiftyDaysFromNow = today.add(const Duration(days: 50));
// adds 1 days
DateTime _future = DateTime.now().add(const Duration(days: 1));
//substracts 1 day
DateTime _tomorrow2 = DateTime.now().subtract(const Duration(days: 1));
Also this, credit ,define the base time, let us say:
var date = new DateTime(2018, 1, 13);
Now, you want the new date:
var newDate = new DateTime(date.year, date.month - 1, date.day);
And you will get
2017-12-13
Y'all are going about it wrong. Presuming 24 hours in a day, or 30 days in a month, is just wrong. Here's how to always get midnight the first of the month, 7 months before today:
void main() {
var n = DateTime.now();
print(DateTime(n.year, n.month - 7, 1));
}
Just use DateTime constructors. They wrap around just fine. Works at month's end as well:
void main() {
var n = DateTime(2021, 2, 28);
print(DateTime(n.year, n.month, n.day + 1));
n = DateTime(2020, 2, 28);
print(DateTime(n.year, n.month, n.day + 1));
}
Which correctly shows 3/1 for 2021, and 2/29 for 2020, as it was a leap year.
Stop adding 24-hour days! I've got a video that explains why.... https://www.youtube.com/watch?v=usFSVUEadyo
And here's a video that goes into this with more detail: Proper Month and Day Arithmetic in Dart and Flutter: youtu.be/LpoBYgzKVwU

Convert month names to numbers in r

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] )

AppleScript : calcule how many day from two date

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 !).