How to declare a global variable? - ios

This is a very simple question, I am getting the day of a week using a snippet
if let weekday = getDayOfWeek("2017-05-01") {
print(weekday)
} else {
print("bad input")
}
func getDayOfWeek(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: todayDate)
return weekDay
}
I need to create "weekday" as global variable can any one help.
What I did is:
class ViewController: UIViewController{
var weekday = NSInteger()
}
I am getting some error in snippet while declaring as above.

Declare currentWeekDay type to Int.
var weekday = Int()
Now access this instance property with if let.
if let weekday = getDayOfWeek("2017-05-01") {
//weekday has block scope and available only with in this if block
self.weekday = weekday
} else {
print("bad input")
}
Edit: Set timezone to get correct weekday
func getDayOfWeek(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone(abbreviation: "UTC")!
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: todayDate)
return weekDay
}

Related

newDateFormatter.string(from: ) is always nil

I am formatting a randomly generated future date but it always returns nil even if the format of dateString is matching and has a value.
But if I try with only "(Date())" instead of newDate, it is successful.
let byDays = Int.random(in: 0...30)
var components = DateComponents()
components.day = byDays
let newDate = String(describing: Calendar.current.date(byAdding: components, to: Date()))
//give the current date output in string
let dateFormatterGet = DateFormatter()
dateFormatterGet.isLenient = true
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
dateFormatterGet.locale = Locale(identifier: "en_US_POSIX")
//describe the new format
guard let date = dateFormatterGet.date(from: newDate) else {
return ""
}
let newDateFormatter = DateFormatter()
newDateFormatter.dateFormat = "MMM dd"
let newStr = newDateFormatter.string(from: date)
print(newStr)
I want the date optional(2019-07-23 17:44:23 +0000) to be printed as Jul 23.
I don't understand the purpose of String(describing: ... You can use the date from the Calendar right away:
func randomFutureDate() -> String? {
let day = Int.random(in: 0...30)
var components = DateComponents()
components.day = day
guard let date = Calendar.current.date(byAdding: components, to: Date()) else {
return nil
}
let newDateFormatter = DateFormatter()
newDateFormatter.dateFormat = "MMM dd"
return newDateFormatter.string(from: date)
}

Create string array of dates from Dates range

I have Date() properties. startingAt and endingAt. And an array of Date(), which are alreadyRegistred. I have to create an array of strings with dates between startingAt and endingAt. StartingAt and endingAt are included and the last requirement is to exclude alreadyRegistred dates.
Do you have some elegant idea, how to do it? Thanks for help!
Edit: Maximum number of dates in final array will be about 7 days.
Dont forget that a Date is basically just a timestamp, and that you can have access to the addingTimeInterval(_:) method.
Knowing that, is very easy to do some calculation between two dates.
I do not have the whole knowledge about your required business logic, but here is a naive implementation that generates Dates between two dates. I'm sure you can run it in a playground and explore a little bit.
import UIKit
func intervalDates(from startingDate:Date, to endDate:Date, with interval:TimeInterval) -> [Date] {
guard interval > 0 else { return [] }
var dates:[Date] = []
var currentDate = startingDate
while currentDate <= endDate {
currentDate = currentDate.addingTimeInterval(interval)
dates.append(currentDate)
}
return dates
}
let startingDate = Date() // now
let endDate = Date(timeIntervalSinceNow: 3600 * 24 * 7) // one week from now
let intervalBetweenDates:TimeInterval = 3600 * 3// three hours
let dates:[Date] = intervalDates(from: startingDate, to: endDate, with: intervalBetweenDates)
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
let dateStrings = dates.map{dateFormatter.string(from: $0)}
print("NOW : \(startingDate)")
for (index, string) in dateStrings.enumerated() {
print("\(index) : \(string)")
}
print("END DATE : \(endDate)")
Try this and see:
// Start & End date string
let startingAt = "01/01/2018"
let endingAt = "08/03/2018"
// Sample date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
// start and end date object from string dates
var startDate = dateFormatter.date(from: startingAt) ?? Date()
let endDate = dateFormatter.date(from: endingAt) ?? Date()
// String date array, to be excluded
let alreadyRegistred = ["01/01/2018", "15/01/2018", "10/02/2018", "20/02/2018", "05/03/2018"]
// Actual operational logic
var dateRange: [String] = []
while startDate <= endDate {
let stringDate = dateFormatter.string(from: startDate)
startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date()
if (alreadyRegistred.contains(stringDate)) {
continue
} else {
dateRange.append(stringDate)
}
}
print("Resulting Array - \(dateRange)")
Here is result:
Resulting Array - ["02/01/2018", "03/01/2018", "04/01/2018", "05/01/2018", "06/01/2018", "07/01/2018", "08/01/2018", "09/01/2018", "10/01/2018", "11/01/2018", "12/01/2018", "13/01/2018", "14/01/2018", "16/01/2018", "17/01/2018", "18/01/2018", "19/01/2018", "20/01/2018", "21/01/2018", "22/01/2018", "23/01/2018", "24/01/2018", "25/01/2018", "26/01/2018", "27/01/2018", "28/01/2018", "29/01/2018", "30/01/2018", "31/01/2018", "01/02/2018", "02/02/2018", "03/02/2018", "04/02/2018", "05/02/2018", "06/02/2018", "07/02/2018", "08/02/2018", "09/02/2018", "11/02/2018", "12/02/2018", "13/02/2018", "14/02/2018", "15/02/2018", "16/02/2018", "17/02/2018", "18/02/2018", "19/02/2018", "21/02/2018", "22/02/2018", "23/02/2018", "24/02/2018", "25/02/2018", "26/02/2018", "27/02/2018", "28/02/2018", "01/03/2018", "02/03/2018", "03/03/2018", "04/03/2018", "06/03/2018", "07/03/2018", "08/03/2018"]
let startDate = Date()
let endDate = Date().addingTimeInterval(24*60*60*10) // i did this to get the end date for now
var stringdateArray = [String]()
if let days = getNumberofDays(date1: startDate, date2: endDate) {
for i in 0...days-1 {
let date = startDate.addingTimeInterval(Double(i)*24*3600)
let stringDate = getStringDate(fromDate: date, havingFormat: "yyyy-MM-dd")
if !(alreadyRegisteredArray.contains(stringDate)) { // checking if already registered
stringdateArray.append(stringDate)
}
}
}
and our helper method
let dateFormatter = DateFormatter()
func getStringDate(fromDate: Date,havingFormat: String) -> String {
dateFormatter.dateFormat = havingFormat
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let date = dateFormatter.string(from: fromDate)
return date
}
func getNumberofDays(date1: Date, date2: Date) -> Int? {
let calendar = NSCalendar.current
let date1 = calendar.startOfDay(for: date1)
let date2 = calendar.startOfDay(for: date2)
let components = calendar.dateComponents([.day], from: date1, to: date2)
return components.day
}

How to print name of the day of the week?

Im trying to print out the name of the day of the week i.e Monday, Tuesday, Wednesday. I currently have this bit of code that does just that. I was wondering if there is a way to get rid of my switch statement and make this better. Thanks!
func getDayOfWeek(_ today: String) -> String? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: todayDate)
switch weekDay {
case 1:
return "Sunday"
case 2:
return "Monday"
case 3:
return "Tuesday"
case 4:
return "Wednesday"
case 5:
return "Thursday"
case 6:
return "Friday"
case 7:
return "Saturday"
default:
return ""
}
}
getDayOfWeek("2018-3-5")
This prints out "Monday"
You are using the wrong date format. The correct format is "yyyy-M-d". Besides that you can use Calendar property weekdaySymbols which returns the weekday localized.
func getDayOfWeek(_ date: String) -> String? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-M-d"
formatter.locale = Locale(identifier: "en_US_POSIX")
guard let todayDate = formatter.date(from: date) else { return nil }
let weekday = Calendar(identifier: .gregorian).component(.weekday, from: todayDate)
return Calendar.current.weekdaySymbols[weekday-1] // "Monday"
}
Another option is to use DateFormatter and set your dateFormat to "cccc" as you can see in this answer:
extension Formatter {
static let weekdayName: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "cccc"
return formatter
}()
static let customDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-M-d"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
}
extension Date {
var weekdayName: String { Formatter.weekdayName.string(from: self) }
}
Using the extension above your function would look like this:
func getDayOfWeek(_ date: String) -> String? { Formatter.customDate.date(from: date)?.weekdayName }
Playground testing:
getDayOfWeek("2018-3-5") // Monday
Date().weekdayName // Thursday
Use this function:
func DayOfWeek(date: String) -> String?
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-M-d"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
guard let _date = dateFormatter.date(from: date) else { return nil }
let weekday = Calendar(identifier: .gregorian).component(.weekday, from: _date)
return Calendar.current.weekdaySymbols[weekday-1]
}

NSDate Extension not working

This is a piece of code that was working in earlier version of swift. It is now giving an error (Cannot convert value of type 'NSDate' to type 'NSDate.Date' in coercion)
extension NSDate {
struct Date {
static let formatterISO8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.ISO8601)! as Calendar
formatter.locale = NSLocale.current
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone!
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
return formatter
}()
}
var formattedISO8601: String { return Date.formatterISO8601.string(from: self as Date) }
}
Issue is that in Swift 3 there is already a structure define with named Date.
So what you can do is change your struct name to MyDate or something else and you all set to go.
Also it is better if you use new Date, Calendar and TimeZone instead of NSDate, NSCalendar and NSTimeZone.
Or make extension of Date like this way.
extension Date {
static let formatterISO8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale.current
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
return formatter
}()
var formattedISO8601: String { return Date.formatterISO8601.string(from: self) }
}
Extensions for both NSDate and Date.
extension Date {
static let formatterISO8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.ISO8601)! as Calendar
formatter.locale = NSLocale.current
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone!
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
return formatter
}()
func formattedISO8601() -> String {
return Date.formatterISO8601.string(from: self)
}
}
extension NSDate {
func formattedISO8601() -> String {
return Date.formatterISO8601.string(from: self as Date)
}
}
And use it like this ...
// NSDate
let nsdate = NSDate.init()
let formattedDate = nsdate.formattedISO8601()
// Date
let date = Date.init()
let formattedNsDate = date.formattedISO8601()
try this
extension Date {
static let formatterISO8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.ISO8601)! as Calendar
formatter.locale = NSLocale.current
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) as TimeZone!
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
return formatter
}()
var formattedISO8601: String { return Date.formatterISO8601.string(from: self)
}
}

NSDate - Returning the closest hour

I want to create a project where:
I create a task to do.
I create a stoping date for this task
I create three reminders using date picker
This I've done already. But now, I want the label to return for me the hour of the closest reminder (but I want the label to do this until the stoping date)
What I accomplish already is a playground project:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-03 16:30")
var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
var datesArray = [firstDate, secondDate, thirdDate]
let firstInterval = firstDate?.timeIntervalSinceDate(currentDate!)
let secondInterval = secondDate?.timeIntervalSinceDate(currentDate!)
let thirdInterval = thirdDate?.timeIntervalSinceDate(currentDate!)
let intervalArray = [firstInterval, secondInterval, thirdInterval]
var aboveZeroIntervals = [NSTimeInterval]()
for interval in intervalArray {
if interval > 0 {
aboveZeroIntervals.append(interval!)
}
}
//print(aboveZeroIntervals)
for date in datesArray {
if date?.timeIntervalSinceDate(currentDate!) == aboveZeroIntervals.minElement() {
print(dateFormatter.stringFromDate(date!))
}
}
How to get only hours from first/second/thirdDate and display the hour from those three dates that is the closest to currentDate hour? And perfectly finish displaying it after currentDate exceeds stopDoingDate...
I came up with something like this (code below). Can anyone look at it and tell me if there is some way I can simplify this?
extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}
//Return Result
return isGreater
}
}
let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-04 16:30")
let stopDoingDate = dateFormatter.dateFromString("2016-01-06 15:00")
if stopDoingDate!.isGreaterThanDate(currentDate!) {
let currentDateComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute], fromDate: currentDate!)
currentDateComponents.hour
currentDateComponents.minute
let dateTimeFormatter = NSDateFormatter()
dateTimeFormatter.dateFormat = "HH:mm"
let currentOnlyTime = dateTimeFormatter.dateFromString("\(currentDateComponents.hour):\(currentDateComponents.minute)")
var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
var firstDateComponents = calendar.components([.Hour, .Minute], fromDate: firstDate!)
var firstDateTime = dateTimeFormatter.dateFromString("\(firstDateComponents.hour):\(firstDateComponents.minute)")
var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
var secondDateComponents = calendar.components([.Hour, .Minute], fromDate: secondDate!)
var secondDateTime = dateTimeFormatter.dateFromString("\(secondDateComponents.hour):\(secondDateComponents.minute)")
var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
var thirdDateComponents = calendar.components([.Hour, .Minute], fromDate: thirdDate!)
var thirdDateTime = dateTimeFormatter.dateFromString("\(thirdDateComponents.hour):\(thirdDateComponents.minute)")
var datesArray = [firstDateTime, secondDateTime, thirdDateTime]
let firstInterval = firstDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let secondInterval = secondDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let thirdInterval = thirdDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let intervalArray = [firstInterval, secondInterval, thirdInterval]
var aboveZeroIntervals = [NSTimeInterval]()
for interval in intervalArray {
if interval > 0 {
aboveZeroIntervals.append(interval!)
}
}
//print(aboveZeroIntervals)
for date in datesArray {
if date?.timeIntervalSinceDate(currentOnlyTime!) == aboveZeroIntervals.minElement() {
let dateTime = calendar.components([.Hour, .Minute], fromDate: date!)
print(dateFormatter.dateFromString("\(currentDateComponents.year)-\(currentDateComponents.month)-\(currentDateComponents.day) \(dateTime.hour):\(dateTime.minute)")!)
print("Next todo hour is: \(dateTime.hour): \(dateTime.minute)")
}
}
} else {
print("Todo DONE")
}
I came up with this:
The only problem is to generate array of dates from currentDate to stopDoingDate but I will handle this alone (and probably post a solution here as well)
I used one of this forum post to generate extension to NSDate:
extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}
//Return Result
return isGreater
}
}
//let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-04 14:00")!
let stopDoingDate = dateFormatter.dateFromString("2016-01-06 16:00")!
let date1 = dateFormatter.dateFromString("2016-01-04 09:00")
let date2 = dateFormatter.dateFromString("2016-01-04 15:00")
let date3 = dateFormatter.dateFromString("2016-01-05 09:00")
let date4 = dateFormatter.dateFromString("2016-01-05 15:00")
let date5 = dateFormatter.dateFromString("2016-01-06 09:00")
let date6 = dateFormatter.dateFromString("2016-01-06 15:00")
var dates: [NSDate] = [date1!, date2!, date3!, date6!, date5!, date4!]
if stopDoingDate.isGreaterThanDate(currentDate) {
var datesAfterCurrentDate = [NSDate]()
for date in dates {
if date.isGreaterThanDate(currentDate) {
datesAfterCurrentDate.append(date)
}
}
let sortedDatesAfterCurrentDate = datesAfterCurrentDate.sort({ $0.timeIntervalSince1970 < $1.timeIntervalSince1970 })
let dateToDisplay = sortedDatesAfterCurrentDate.first!
let timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = .ShortStyle
print(timeFormatter.stringFromDate(dateToDisplay)) } else {
print("We are done here")
}
This forum is truly awesome! Thanks Leo Dabus for the simple idea!

Resources