Need unique dateformatter for single number (day & month) [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Input: 9(Wed).12(Dec).2020
Day and month are represented by one number
I need date formatter for above input date string

You can try
func getDateFrom(_ str:String) -> Date? {
let dayTimePeriodFormatter = DateFormatter()
dayTimePeriodFormatter.dateFormat = "dd(EEE).MM(MMM).yyyy"
let date = dayTimePeriodFormatter.date(from: str)
return date
}
and call it getDateFrom("9(Wed).12(Dec).2020")

Related

Which way is recommended to use guard let? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to check preconditions for proceeding a function in iOS/Swift.
Option 1:
guard let name = str["name"], let age = str["age"] else {
print("name/age missing")
return
}
Option 2:
guard let name = str["name"] else {
print("name missing")
return
}
guard let age = str["age"] else {
print("age missing")
return
}
Which option is recommended.
This is completely unrelated to Swift.
From a UI / UX perspective certainly the 2nd option since you can now point to the exact input field that is missing.

Check if a string is base64Encoded Swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How can I check if the string is base64 encoded in swift?
Input = "tNC6umcfBS/gelbo2VJF3i4LAhUKMp4oDHWN5KyYUTWeJIQKKYx6oAcQnGncIrPJNC1tUYMKV4kJQj3q9voIOrxc1n7FmRFvDXeRgWGNcGYO66dH3VjoEgF0oxZOpfzwSZKSv3Jm7Q=="
let base64Regex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"
let predicate = NSPredicate(format: "SELF MATCHES %#", base64Regex)
let result = predicate.evaluate(with: "InputString")
You can also try to decode using the built-in Data:
let inputString = "..."
let isBase64Encoded = Data(base64Encoded: inputString) != nil

Detecting time range in ios using Swift [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 6 years ago.
Improve this question
Lets say I have some rules for displaying a message like below
between 00:00-12:00 -> morning
between 12:01-14:00 -> noon
between 14:01-17:00 -> afternoon
between 17:00-23:59 -> evening
if current mobile time is between 00:00 and 12:00 I should get morning
How can I do that please guide.
This should do the job
func check(time: NSDate) -> String? {
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm"
formatter.timeZone = NSTimeZone(name: "GMT")
guard let
beginNoon = formatter.dateFromString("12:00"),
beginAfternoon = formatter.dateFromString("14:00"),
beginEvening = formatter.dateFromString("17:00")
else { return nil }
if time.compare(beginNoon) == .OrderedAscending { return "Morning" }
if time.compare(beginAfternoon) == .OrderedAscending { return "Noon"}
if time.compare(beginEvening) == .OrderedAscending { return "Afternoon" }
return "Evening"
}
Test
check(formatter.dateFromString("10:00")!) // "Morning"
check(formatter.dateFromString("13:00")!) // "Noon"
check(formatter.dateFromString("15:00")!) // "Afternoon"
check(formatter.dateFromString("22:00")!) // "Evening"
Considerations
Your ranges are inconsistent
Here you are including the left and right boundaries
00:00-12:00 -> morning
here you are including only the right boundary
12:01-14:00 -> noon
14:01-17:00 -> afternoon
and here only the left boundary :)
between 17:00-23:59
In my code, the left boundary in only included while the right one is excluded.

In swift ,I need to show “10月02日”but not "10月2日",how to transfer "2"to "02"? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In swift ,I need to show “10月02日”**but not **"10月2日",how to transfer "2"to "02"?
Now , i have wrote these codes,but it will show "2",but not "02":
var publishLastDate:NSDate = momentLast.created_on
println(publishLastDate)
componentsLast = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear, fromDate: publishLastDate)
lastDay = componentsLast.day
lastMonth = componentsLast.month
lastYear = componentsLast.year
It looks like you're grabbing the components and printing the string yourself. You can format it like so:
let string = String(format: "%02d月%02d日", lastMonth, lastDay)

How to get data from UIDatePicker - Swift [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
I'm trying to get the data from the UIDatePicker, for example the user picked a date as 7:15 PM
How do I get the data from that so I can manipulate and use it for some other function?
If you have any questions or need any clarifications please comment them down below.
Add a handler for your UIDatePicker like so:
yourDatePicker.addTarget(self, action: #selector(DatePickerViewController.handler(sender:)), for: UIControl.Event.valueChanged)
And then do something like this in your handler:
#objc func handler(sender: UIDatePicker) {
let timeFormatter = DateFormatter()
timeFormatter.timeStyle = DateFormatter.Style.short
var strDate = timeFormatter.string(from: yourDatePicker.date)
// do what you want to do with the string.
}

Resources