Flutter how to add a 0 behind a hour if the hour is less than 10? [closed] - flutter

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Im trying to check if an hour is below 10 so i can add a 0 behind it to make it the right hour format.
All my efforts till now haven't worked out.
Thank you for you time

You can use padLeft() method on strings. It takes width and padding parameters. You can use it like this:
print("5".padLeft(2,"0")); // Prints 05
print("14".padLeft(2,"0")); // Prints 14

you can try this;
var now = new DateTime.now();
String newHour = now.hour.toString();
if (newHour.length > 2) {
newHour = "0"+ newHour;
}

Related

How to loop through range of dates in Swift3 (for each day between x and y, call on a method and pass in that day as a date value) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to do the following:
for each day between startdate and enddate, call on this method and pass in that day as the date...etc.
This seems like something that should be straightforward but everything I'm trying is just not working out. I am unfamiliar with swift (this is a project I'm thrown into), but it seems like something as easy as a for loop is more complicated than I thought it was.
A lot of what I tried researching is either deprecate methods, or it's more focused on calculating days between two dates or something like that which I don't need.
p.s. I may need a fairly large range of dates (more than a year), so I think there would have to be consideration for things like leap years with certain methods?
If anyone can help that'd be really appreciated, thank you
Any additional info that I'm not sure if is relevant: I am using xcode version 9.4.1, ios 11.4. I have seen that people use c in these projects but I'm not too sure how that would work, it would be great to have just the swift code for this
This should be pretty simple. Just get the difference in days, create a range with it and loop through the number of days, add it to the start date and call your method. Note that I am using noon time as it is recommended for time insensitive calendrical calculations:
extension Date {
var noon: Date {
Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
}
let startOf2020 = DateComponents(calendar: .current, year: 2020, month: 1, day: 1).date!.noon
let now = Date().noon
let days = Calendar.current.dateComponents([.day], from: startOf2020, to: now).day! // 302
(0...days).forEach { n in
let date = Calendar.current.date(byAdding: .day, value: n, to: startOf2020)!
print(date.description(with:.current))
}

How to stop VS. Code from destroying the formating of my code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Is there some way to write/download rules for how VS. Code should format the code?
Example:
I have this nicely formatted (JavaScript) code:
var boundingBox = Object.create(null);
boundingBox.top = 0;
boundingBox.left = 0;
boundingBox.right = window.innerWidth;
boundingBox.bottom = window.innerHeight;
But after I do: Right-click --> Format Document
It destroys it into this (relatively) ugly & "unreadable" mess:
var boundingBox = Object.create(null);
boundingBox.top = 0;
boundingBox.left = 0;
boundingBox.right = window.innerWidth;
boundingBox.bottom = window.innerHeight;
Is there some plugin/.edditorconfig rules/settings/other thing I can do to tell VS. Code how it should format the code? (or at least not destroy the existing formating?)
I think what you want is this extension:
https://marketplace.visualstudio.com/items?itemName=wwm.better-align
It allows you to align your code lines by colon(:), assignment(=,+=,-=,*=,/=) and arrow(=>)
However, my personal opinion is that it would likely be worth it to get used to other code formatting styles. From my experience, aligning code by specific characters is not common practice.

How to fix button clicked not updating but others do [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm making a math game. Every time I click a button a new question should appear with different results to choose from.
However every time I click the button which I think is the result it doesn't update the text but the others do.
See github: https://github.com/Combii/BrainTrainerSwift
Use UIButton.setTitle(, for:) method. I suggest you to change code in ViewController.swift:
func setNumbers(numberDic: Dictionary<String, Int>) {
let btns = [bt1, bt2, bt3, bt4]
UIView.performWithoutAnimation {
for (position, number) in numberDic {
btns[(Int(position) ?? 0) - 1]?.setTitle(String(number), for: .normal)
}
}
}

yahoo finance API stopped working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Anyone else having problems with this API? I have just recently started working with it but suddenly everything that was working both mine and others is no longer working, first the CSV interface and now the YQL interface. Surprised I don't see any mention of this on this board or a google search.
You can still back out the periods since the interval is in seconds (60*60*24), one day = 86,400.
The cookie is the problem. This is how I did it in VBA: https://stackoverflow.com/a/44055850/8027976
Example for period 1:
Dim baseDate As Date: baseDate = #1/1/1970#
Dim period1 As Long: period1 = (startDate - baseDate) * 86400
Dim period2 As Long: period2 = (endDate - baseDate + 0.8) * 86400
You need the ".8", otherwise, it will not pull the end-of-day price.
Yes, it's not working anymore. They changed the coding so now it reads something like
https://query1.finance.yahoo.com/v7/finance/download/IWM?period1=1492465014&period2=1495057014&interval=1d&events=history&crumb=oL864EniL6D
evidently they want to plant a cookie so it's not possible to automate it. Also there is no clear equivalence between the period1 and period2 numbers and the dates (in this case 5/17/2016 and 5/17/2017 respectively) so you couldn't even program the dates.
I am not sure why. I am grateful for the years of downloading data, and I would love to have this ability restored.
The periods seem to be the number of seconds between epoch and the start of the day in UTC. In JavaScript the way to compute the periods is as follows:
new Date(2017,4,18).valueOf()/1000 - new Date(2017,4,18).getTimezoneOffset()*60

Unable to set member variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Very strange issue.
I have the following code:
NSDictionary* notificationUserInfo = [pNotification userInfo];
NSManagedObject* newShoppingFilter = [notificationUserInfo valueForKey:#"shoppingListFilter"];
self.shoppingListFilter = newShoppingFilter;
NSLog(#"%# tapped", [newShoppingFilter valueForKey:#"name"]);
For some reason the self.shoppingListFilter = newShoppingFilter is not setting the variable.
I assume that this is some issue with not initializing the self.shoppingListFilter variable in some way but I cannot figure this out. The NSLog shows the right output, newShoppingFilter is not null but self.shoppingListFilter is.
Any help is appreciated.
I bet newShoppingFilter is nil. Most likely, there is no key "shoppingListFilter" in the notification user info dictionary.
Set a breakpoint at the line that assigns a value to self.shoppingListFilter and check the value of newShoppingFilter. Also display the entire contents of notificationUserInfo.
Post the code that creates the user info dictionary and passes it into the notification that you are posting. That would help track down the problem.