How to save UIDatePicker Date and print it back out? [closed] - iphone

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a UIDatePicker on my ViewController, and When I click the save button, I want it to save the date to a variable, allowing me to print the content of the variable, which should be the date that it was set.
I have a model(Class) to save all info on my ViewController (UiTextField text, etc...), I am just not sure how to save a date from the DatePicker and print it out. Can anyone help me figure how to make it work?
This is my first app(Besides Hello World)

NSDate* pickerDate = picker.date;
NSLog(pickerDate); // or NSLog(#"Date: %#", pickerDate);
UIDatePicker Documentation

A'sa Dickens already showed how to get a date from your date picker. An NSDate object is a property list object, so it can be written to a plist or user defaults directly. NSDate also conforms to NSCoding, so you can add NSDate objects to archives.
You can also convert an NSDate to a double using the NSDate instance method timeIntervalSinceReferenceDate, and convert from a double to a date using the NSDate class method dateWithTimeIntervalSinceReferenceDate.

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

Flutter how to add a 0 behind a hour if the hour is less than 10? [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
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;
}

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.

iPhone: Converting Oracle Timestamp to NSDate or to Unix Timestamp (e.g. double)

Is there a good+easy way to convert an Oracle Timestamp to an NSDate object on the iPhone? I have asked my customer to give me a DB with timestamps as Unix Timestamp (doubles with 0 = start of 1970) but it seems to be a problem for them. Thanks.
Note: You can easily convert a Unix Timestamp to an NSDate with
[NSDate dateWithTimeIntervalSince1970: timestamp] // timestamp is a "double"
What I need to do is convert the Oracle timestamp to a double, aka Unix Timestamp. The rest is easy.
More info: I need Objective-C code to run on the iPhone using NSStrings that represent dates in Oracle Timestamp format. Another StackOverflow thread suggests using NSDateFormatter, but I don't know what format to use and the initialization method for the formatter suggested in that thread generates a warning for me.
I'm not sure about the converting to an NSDate, but you can convert an Oracle date to a unix timestamp easily. The following SQL snippet will do it:
(my_date - to_date('01/01/1970','DD/MM/YYYY')) * 86400
It looks like my best bet is to use the following:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm a"];
I'm not sure how much variance there is in Oracle timestamps, so if there is a problem with the above approach, I'd love to know.
Thanks for the other answers. I will leave the question open to invite other approaches.
It's a shame that you accepted that answer already but this is the best way to do it:
NSDate *dateTraded = [NSDate dateWithTimeIntervalSince1970 :[timestampVal integerValue]];
...where timestampVal is an NSString object representing a timestamp value.