// BadLuckDays.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2016-11-08 File created. 2016-11-08 Last modification. // This program demonstrates: // - use of standard types Date, Calendar, etc. import Foundation let calendar = Calendar.current let date_of_today = Date() let year_today = calendar.component( Calendar.Component.year, from: date_of_today ) let month_today = calendar.component( Calendar.Component.month, from: date_of_today ) var components_of_date_to_test = DateComponents() components_of_date_to_test.year = year_today components_of_date_to_test.month = month_today components_of_date_to_test.day = 13 var date_to_test = calendar.date( from: components_of_date_to_test )! let date_formatter = DateFormatter() print( "\n The following are Fridays which also are " + "\n the 13th days of a month: \n" ) var number_of_friday13_dates_to_print = 10 while number_of_friday13_dates_to_print > 0 { let day_of_week_of_date_to_test = calendar.component( Calendar.Component.weekday, from: date_to_test ) if day_of_week_of_date_to_test == 6 { let year_of_date_to_test = calendar.component( Calendar.Component.year, from: date_to_test ) let month_of_date_to_test = calendar.component( Calendar.Component.month, from: date_to_test ) print( String( format: " %04d-%02d-13, %@", year_of_date_to_test, month_of_date_to_test, date_formatter.weekdaySymbols[ day_of_week_of_date_to_test - 1 ] ) ) number_of_friday13_dates_to_print -= 1 } // The following statement creates a new Date object that contains // the 13th day of the following month. date_to_test = calendar.date( byAdding: Calendar.Component.month, value: 1, to: date_to_test )! } print( "\n" )