I'm getting last week dates from below code, week starts with Saturday. It's getting dates(2019-04-27 18:30:00 +0000, 019-05-04 18:30:00 +0000), but when i try to set DateFormatter it's getting dates(28-04-2019 00:00:00, 05-05-2019 00:00:00). I want week starts with with Monday in india local time & date.
var dateFormatter:DateFormatter!
var date:Date!
dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
date = Date()
var calendar = Calendar.current
calendar.firstWeekday = 2 // 1 is Sunday, 2 is Monday
let lastWeek = calendar.date(byAdding: .weekOfYear, value: -1, to: Date())
if let lastWeek = lastWeek {
var startOfLastWeek = Date()
var interval = TimeInterval(0)
_ = Calendar.current.dateInterval(of: .weekOfYear, start: &startOfLastWeek, interval: &interval, for: lastWeek)
let endOfLastWeek = startOfLastWeek.addingTimeInterval(interval)
print(startOfLastWeek)//2019-04-27 18:30:00 +0000
print(endOfLastWeek)//019-05-04 18:30:00 +0000
let startWeekString = dateFormatter.string(from: startOfLastWeek)
let endWeekString = dateFormatter.string(from: endOfLastWeek)
print(startWeekString)//28-04-2019 00:00:00
print(endWeekString)//05-05-2019 00:00:00
The Date is always in UTC in any programming language, It does't have time zones. And DateFormatter is used to show Date in local/localised format by providing timezones.
The Date Formatter allow you to convert it to string that is human readable.
In phones:
If you have central server/DB then you should save you Dates in UTC.
when time to show on your phone, convert UTC to local using time zone.
DO NOT TRY TO OVERCOMPLICATE IT.
Use below code to find Monday and Sunday of last week:
var date:Date!
dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.timeZone=TimeZone.current
TimeZone.current
date = Date()
var calendar = Calendar.current
calendar.firstWeekday = 2 // 1 is Sunday, 2 is Monday
let lastWeek = calendar.date(byAdding: .weekOfYear, value: -1, to: Date())
if let lastWeek = lastWeek {
var startOfLastWeek = Date()
var interval = TimeInterval(0)
_ = Calendar.current.dateInterval(of: .weekOfYear, start: &startOfLastWeek, interval: &interval, for: lastWeek)
startOfLastWeek = calendar.date(byAdding: .weekday, value: 1, to: startOfLastWeek)!
print(interval)
interval = interval - 1
let endOfLastWeek = startOfLastWeek.addingTimeInterval(interval)
print(startOfLastWeek)//2019-04-27 18:30:00 +0000
print(endOfLastWeek)//019-05-04 18:30:00 +0000
let startWeekString = dateFormatter.string(from: startOfLastWeek)
let endWeekString = dateFormatter.string(from: endOfLastWeek)
print(startWeekString)//28-04-2019 00:00:00
print(endWeekString)//05-05-2019 00:00:00
}
Related
Lets say i have a program that reminds users of their appointments , from the current date until the date of the appointment, i want to find out if a particular date is a week to the appointment or a month to the appointment .
var startDate = startDate
let calendar = Calendar.current
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd"
while startDate <= endDate {
var newDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
if newDate is a month to endDate {
//schedule reminder
}
if newDate is a week to endDate{
//schedule reminder
}
how can i check if the current date is a week/month to the appointment ?
You don't need to use any date comparison, you can simply generate the notification dates using Calendar.date(byAdding:value:to:) and just passing the correct components. To set the date 1 week/month before endDate, pass -1 to value.
let oneWeekBeforeAppointment = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: endDate)!
let oneMonthBeforeAppointment = Calendar.current.date(byAdding: .month, value: -1, to: endDate)!
Try this to calculate the duration in days
func DateFormat() -> DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
return dateFormatter
}
var appointementDate: Date?
var today = Date()
appointementDate = DateFormat().date(from: "22/02/2020")
today = DateFormat().date(from: DateFormat().string(from: today))!
let timeInterval = Int(exactly: (today.timeIntervalSince(appointementDate!))) ?? 0
print("\(timeInterval/86400) days left")
I want to get current time according to my iPhone device I am using code which is giving me yesterday time.
My code is
let calendar = Calendar.current
let dateStart = calendar.startOfDay(for: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.timeZone = TimeZone.current
let strDate = Date().description(with: Locale.current) //dateFormatter.string(from: dateStart)
print("date Start ***** (dateStart)")
By this I am getting a response :
date Start ***** 2018-02-14 18:30:00 +0000
Can anyone help me how can I get correct time today is 15-02-2018 18:00
Thanks
Try this :
let calendar = Calendar.current
let dateStart = calendar.startOfDay(for: Date()) // Feb 15, 2018 at 12:00 AM"
print(dateStart) // 2018-02-14 18:30:00 +0000
let dateFormatter = DateFormatter()
var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "UTC" }
dateFormatter.locale = Locale.init(identifier: localTimeZoneAbbreviation)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let newDate = dateFormatter.string(from: dateStart)
print(newDate) // 2018-02-15 00:00
Update :
// Current Time
dateStart = Date()
newDate = dateFormatter.string(from: dateStart)
print(newDate) // 2018-02-15 22:30
I am getting two time stamps from server response. Like following
value = 1507824000; //2017-10-12 16:00:00 +0000
value2 = 1507939200; //2017-10-14 00:00:00 +0000
let startTimeStamp = dateTimeStampValues["value"] as! String
let endTimeStamp = dateTimeStampValues["value2"] as! String
let convertedDate = Double(startTimeStamp)
let convertedEndDate = Double(endTimeStamp)
var startdate = Date(timeIntervalSince1970: convertedDate!)
let enddate = Date(timeIntervalSince1970: convertedEndDate!)
self.showRange(between: startdate, and: enddate)
func showRange(between startDate: Date, and endDate: Date) {
guard startDate < endDate else { return }
let calendar = Calendar.current
let calendarEndDate = calendar.startOfDay(for: endDate)
var currentDate = calendar.startOfDay(for: startDate)
while(currentDate <= calendarEndDate) {
print(currentDate)
currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
let dateString = currentDate.convertDateToString(withFormat: "MMM-dd-yyyy h:mm a")
self.totalDaysFromServerArray.append(dateString)
print("\(self.totalDaysFromServerArray)")
}
}
But, the out put is
self.totalDaysFromServerArray is ["Oct-13-2017 12:00 AM", "Oct-14-2017 12:00 AM", "Oct-15-2017 12:00 AM"]
but, it should print from
Oct-12-2017 to Oct-14-17
Can anyone suggest me, where its going wrong in my code, thanks.
If you will print your startdate and enddate directly(without converting in string) it will be print 2017-10-12 16:00:00 +0000 and 2017-10-14 00:00:00 +0000 respectively because it will be in UTC format. When you will convert that date to string then it will be in your local timezone! For example, If you are in india then your timezone will be GMT + 05:30 or UTC + 05:30, so it will add 05 : 30 hours in your date while converting to string.
So, there is nothing wrong in your code. Your result dates are in gmt + 05 : 30 time zone as you has converted them to string.
Now, If someone in UK do this then he/she will find it in their local timezone! I mean they will get output what you expect in your question because UK's timezone is UTC + 00 : 00 or GMT + 00 : 00
Now if you want to show your string value also in utc or any specific timezone OR if you want to prevent above scenario when you convert date to string then you can set specific timezone! then it will convert date to string in that timezone(see below example for that). But it is not proper way unless you hardly required it because normal behavior is that you shows date in local timezone to users.
For example,
let value = "1507824000" //2017-10-12 16:00:00 +0000
let value2 = "1507939200" //2017-10-14 00:00:00 +0000
let convertedDate = Double(value)
let convertedEndDate = Double(value2)
let startdate = Date(timeIntervalSince1970: convertedDate!)
let enddate = Date(timeIntervalSince1970: convertedEndDate!)
self.showRange(between: startdate, and: enddate)
and your function will be,
func showRange(between startDate: Date, and endDate: Date) {
guard startDate < endDate else { return }
let calendar = Calendar.current
let calendarEndDate = calendar.startOfDay(for: endDate)
var currentDate = calendar.startOfDay(for: startDate)
while(currentDate <= calendarEndDate) {
print(currentDate)
currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
let df = DateFormatter.init()
df.dateFormat = "MMM-dd-yyyy h:mm a"
df.timeZone = TimeZone.init(abbreviation: "BST")
let dateString = df.string(from: currentDate)
totalDaysFromServerArray.append(dateString)
}
print("\(totalDaysFromServerArray)")
}
And your result will be,
Oct-12-2017 7:30 PM Oct-13-2017 7:30 PM Oct-14-2017 7:30 PM
Hope this will help!
I am trying to get a Date object from NSDateComponents, but Calendar.date(from: Components) is giving a date one day before the date that was in my original components.
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let dateComponents = calendar!.components([.Year , .Month , .Weekday, .Day], fromDate: NSDate())
dateComponents.year = 2015
dateComponents.month = 9
dateComponents.day = 1
po calendar!.dateFromComponents(dateComponents)
▿ Optional(2015-08-31 18:30:00 +0000)
- Some : 2015-08-31 18:30:00 +0000
Set the time zone with calendar object to UTC, and the try to get Date from the calendar.
calendar.timeZone = NSTimeZone(name: "UTC")!
It's giving you the UTC time zone, Just convert the Date by usning NSDateFormatter like this, you will get your answer -
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let dateComponents = calendar!.components([.Year , .Month , .Weekday, .Day], fromDate: NSDate())
dateComponents.year = 2015
dateComponents.month = 9
dateComponents.day = 1
let df = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd"
let datestring = df.stringFromDate(calendar!.dateFromComponents(dateComponents)!)
print(datestring)
We can get day of year for date using below line.
let day = cal.ordinalityOfUnit(.Day, inUnit: .Year, forDate: date)
But how can we get the date from day of year?
If you know the year you can get DateComponents date property as follow:
extension Calendar {
static let iso8601 = Calendar(identifier: .iso8601)
}
let now = Date()
let day = Calendar.iso8601.ordinality(of: .day, in: .year, for: now)! // 121
let year = Calendar.iso8601.component(.year, from: now) // 2017
let date = DateComponents(calendar: .iso8601, year: year, day: day).date // "May 1, 2017, 12:00 AM"
or using DateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy D"
if let date = dateFormatter.date(from: "\(year) \(day)") {
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
dateFormatter.string(from: date) // "May 1, 2017, 12:00 AM"
}
You cannot go the other way. Going from a date to a day of the year discards all other information, you are left with only the day of the year (you no longer know what year). To go back to a full date you would have to make assumptions about the year the day was in.
The answer that #LeoDabus gave is more succinct than this, so it is perhaps the better choice. Having said that, this is the code that I would have used:
let dateComponents = NSDateComponents();
dateComponents.year = 2015
dateComponents.day = day
let calendar = NSCalendar.currentCalendar()
let date = calendar.dateFromComponents(dateComponents)
Updated for Swift 4:
let dateComponents = NSDateComponents();
dateComponents.year = 2018
dateComponents.day = someDay
let calendar = NSCalendar.current
let date = calendar.date(from: dateComponents as DateComponents)