Setting a time in Luxon and showing it in the user's local timezone - timezone

I'm using Luxon to show when orders will be processed. If they place an order after business hours 9am-5pm EST, I want to tell them their order will be ready at 2:00pm EST the following day.
let easternTime = luxon.DateTime.local().setZone('America/New_York');
let localTime = luxon.DateTime.local();
// Omitted/Irrelevant Logic to determine if time is after 9am-5pm EST
console.log("Business closed...turnaround next day");
console.log(easternTime.toLocal().plus({ day: 1 }).toFormat('EEE, MMM d, 2:00 a ZZZZ'));
If someone see's this message in San Francisco (PST), I'd like the time to read 11:00am PST. How would I go about accomplishing this with Luxon or some other framework?
Thank you!

Just needed to use the .set({ hour: 14 }) functionality:
console.log(easternTime.set({ hour: 14 }).set({ minutes: 0 }).toLocal().plus({ day: 1 }).toFormat('EEE, MMM d, h:mm a ZZZZ'));

With the Luxon DateTime object you can set time using set
DateTime.now().set({ hour: 0, minute: 0, second: 0, millisecond: 0 })

Related

Adding/Reducing day in the Date object returns wrong date when day light savings time ends

I'm using
Calendar.current.date(byAdding: .day, value: -1, to: somedate) ?? somedate
to reduce 1 day from some date. Since Daylight Saving Time Ended on 1'st Nov, 2020. When i'm trying to do this on 2nd Nov, 2020 0hr:0m:0s's date object, i expect it to return 1st Nov, 2020 0hr:0m:0s, but instead it is returning 31st Oct, 2020 23hr:0m:0s.
Is it something i'm doing wrong or is it some other issue?
How to reproduce:-
Create a date object using time stamp 1604275200. using Date(timeIntervalSince1970: 1604275200)
Change timezone of ur device to some place where daylight savings time is considered. i tried it in HST timezone
Try reducing the day using the above given method.
You'll see date returning as 31st Oct.
extension Date {
init(timeIntervalInMillis: Double) {
self.init(timeIntervalSince1970: timeIntervalInMillis / 1000)
}
func add(_ component: Calendar.Component, value: Int) -> Date {
return Calendar.current.date(byAdding: .day, value: value, to: self) ?? self
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
}
print(Calendar.current.timeZone.identifier)
let date = Date(timeIntervalInMillis: 1604275200000)
print("Date is ",date)
print("Yesterday's date is ",date.add(.day, value: -1))
print("Noon time is ",date.noon)
print("Yesterday date from noon's date is ",date.noon.add(.day, value: -1))
Output:
America/Chicago
Date is 2020-11-02 00:00:00 +0000
Yesterday's date is 2020-10-31 23:00:00 +0000
Noon time is 2020-11-01 18:00:00 +0000
Yesterday date from noon's date is 2020-10-31 17:00:00 +0000
Any help would be appreciated.
edit/update:
There is nothing wrong with your code. Your issue is that you are printing the UTC date representation instead of using DateFormatter with the time zone set to chicago to show the resulting date at the desired timezone.
print("Date is ",date) // Date is 2020-11-02 00:00:00 +0000\n"
print("Yesterday's date is ",date.add(.day, value: -1)) // "Yesterday's date is 2020-10-31 23:00:00 +0000\n"
print("Noon time is ",date.noon) // "Noon time is 2020-11-01 18:00:00 +0000\n"
print("Yesterday date from noon's date is ",date.noon.add(.day, value: -1)) // "Yesterday date from noon's date is 2020-10-31 17:00:00 +0000\n"
let fmter = DateFormatter()
fmter.timeZone = TimeZone(identifier: "America/Chicago")!
fmter.dateStyle = .full
fmter.timeStyle = .full
print("Date is ", fmter.string(from: date)) // "Date is Sunday, 1 November 2020 18:00:00 Central Standard Time\n"
print("Yesterday's date is ", fmter.string(from: date.add(.day, value: -1))) // "Yesterday's date is Saturday, 31 October 2020 18:00:00 Central Daylight Time\n"
print("Noon time is ", fmter.string(from: date.noon)) // "Noon time is Sunday, 1 November 2020 12:00:00 Central Standard Time\n"
print("Yesterday date from noon's date is ",fmter.string(from: date.noon.add(.day, value: -1))) // "Yesterday date from noon's date is Saturday, 31 October 2020 12:00:00 Central Daylight Time\n"

Check if NSDate is in current week not working for specific date?

I have tried couple of ways to check if NSDate is in current week:
Method 1
func isInThisWeek(date: Date) -> Bool
{
return Calendar.current.isDate(date, equalTo: Date(), toGranularity: .weekOfYear)
}
Method 2
func dateFallsInCurrentWeek(date: Date) -> Bool
{
let currentWeek = Calendar.current.component(Calendar.Component.weekOfYear, from: Date())
let datesWeek = Calendar.current.component(Calendar.Component.weekOfYear, from: date)
return (currentWeek == datesWeek)
}
Now here is the case where I am getting FALSE though this date is in current week.
I tested on: Monday, August 10, 2020 6:00:00 PM (My time zone: +5:30 GMT). So as per calendar, this date belongs to 10 Aug - 16 Aug week.
What may be wrong? In my iPad in which I am testing this, has starting day of Week is Monday as following:
All calendars would consider sunday as the first weekday. If you would like to consider monday as the start of your week you need to use iso8601 calendar.
let date = Date(timeIntervalSince1970: 1597516200) // .description // "2020-08-15 18:30:00 +0000"
let tz = TimeZone(secondsFromGMT: 5*3600 + 1800)! // GMT+0530 (fixed)
let components = Calendar.current.dateComponents(in: tz, from: date)
components.day // 16
components.weekday // Sunday
// Current Calendar starts on sunday so it goes from 9...15 and 16...22
// To get the current week starting on monday you need iso8601 calendar
let equalToDate = DateComponents(calendar: .current, timeZone: tz, year: 2020, month: 8, day: 10, hour: 18).date!
equalToDate.description // "2020-08-10 12:30:00 +0000"
Calendar(identifier: .iso8601).isDate(date, equalTo: equalToDate, toGranularity: .weekOfYear) // true

Formatting a date with Moment Tz

I'm struggling with formatting a date to insert into a calendar. I'm working on an Ionic project and I'm attempting to leverage moment.js timezones. The data is being passed into the application in several pieces. I am receiving a millisecond date time stamp, a 24-hour string for time and a string of the timezone. The date does not contain the time. What I want to achieve is creating a date object from these pieces and then converting the date into the user's local timezone to add it to their device calendar.
example of data passed to app
The date of the event: August 14, 2018 17:00
time = 17:00
date = 1534204800
timezone = AEDT
The destination timezone is based on the user's location.
let timeFormatter = new Date();
timeFormatter.setMilliseconds(date);
let momentHrMin = moment(timeFormatter.toDateString() + " " + time);
//WP sever is on GMT get the day, month and yr
let momentTZDate = momentTz.unix(date);
momentTZDate.tz('GMT');
let day = momentTZDate.days();
let month = momentTZDate.month();
let yr = momentTZDate.year();
//set the correct timezone on the ecpoh/unix DATE with momentTz.
momentTZDate.tz(this.eventDetails.eventDetail.timezoneOffset);
// Lastly set the date so the timezone conversions are correct
momentTZDate.set(
{
day: day,
month: month,
year: yr,
hour: momentHrMin.hour(),
minute: momentHrMin.minute(),
second: 0,
millisecond: 0
}
);
I found the cause of my struggles with the timezones. The abbreviated AEDT is not an accepted value for Moment.tz. AEDT could represent multiple timezones. For example, American Eastern, Australian Eastern, etc... Simply changing momentTZDate.tz("AEDT"); to momentTZDate.tz("Australia/Brisbane"); fixed my issue. Below is my typescript code I used to get through this issue. I won't say its the best but it works.
private getCalDate(date:number, time: string): Date {
// create an object in which the hours and minutes can be parse from(do to free text entry on the backend moment handles different inputs)
let timeFormatter = new Date();
timeFormatter.setMilliseconds(date);
let momentHrMin = moment(timeFormatter.toDateString() + " " + time);
//WP sever is on GMT get the day, month and yr
let momentTZDate = momentTz.unix(date);
momentTZDate.tz('GMT');
let day = momentTZDate.days();
let month = momentTZDate.month();
let yr = momentTZDate.year();
//set the correct timezone on the ecpoh/unix DATE with momentTz.
momentTZDate.tz("Australia/Brisbane");
// Lastly set the date so the timezone conversions are correct
momentTZDate.set(
{
day: day,
month: month,
year: yr,
hour: momentHrMin.hour(),
minute: momentHrMin.minute(),
second: 0,
millisecond: 0
}
);
return momentTZDate.toDate();
}

1st april dates of 80s failed to parse in iOS 10.0

I found that DateFormatter date(from:) method can't parse a couple of specific dates. Method returns nil for the 1st april of 1981-1984 years. Is it a bug of Foundation? What can we do to perform parsing of such dates?
Xcode 8.0, iOS SDK 10.0. Here is a screenshot of a short playground example:
This problem occurs if daylight saving time starts exactly on
midnight, as it was the case in Moscow in the years 1981–1984 (see for example Clock Changes in Moscow, Russia (Moskva)).
This was also observed in
Why does NSDateFormatter return nil date for these 4 time zones? and
Why NSDateFormatter is returning null for a 19/10/2014 in a Brazilian time zone?
For example, at midnight of April 1st 1984, the clocks were adjusted one hour forward, which means that the date "1984-04-01 00:00"
does not exist in that timezone:
let dFmt = DateFormatter()
dFmt.dateFormat = "yyyy-MM-dd"
dFmt.timeZone = TimeZone(identifier: "Europe/Moscow")
print(dFmt.date(from: "1984-04-01")) // nil
As a solution, you can tell the date formatter to be "lenient":
dFmt.isLenient = true
and then it will return the first valid date on that day:
dFmt.isLenient = true
if let date = dFmt.date(from: "1984-04-01") {
dFmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
print(dFmt.string(from: date))
}
// 1984-04-01 01:00:00
A different solution
was given by rob mayoff, which is to make the date formatter use noon instead of midnight as the
default date. Here is a translation of rob's code from Objective-C to Swift:
let noon = DateComponents(calendar: dFmt.calendar, timeZone: dFmt.timeZone,
year: 2001, month: 1, day: 1, hour: 12, minute: 0, second: 0)
dFmt.defaultDate = noon.date
if let date = dFmt.date(from: "1984-04-01") {
dFmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
print(dFmt.string(from: date))
}
// 1984-04-01 12:00:00

Creating NSDate from components without "timezone correction"

I have a function
func createDate(day: Int, month: Int, year: Int) -> NSDate {
var comp = NSDateComponents()
comp.day = day
comp.month = month
comp.year = year
var cal = NSCalendar.currentCalendar()
if var date = cal.dateFromComponents(comp) {
return date
}
return NSDate()
}
If I call it with
createDate(07, 01, 1984)
the output is 1984-01-06 23:00:00 +0000. I assume that there is some influence from the time zone. How can I adjust my code so that the output will be 1984-01-07 00:00:00 +0000? What am I getting wrong here? How am I confused?
There is no need to use NSDateComponents to input time. NSCalendar already has a method for you to input your date using your local time. It is called dateWithEra.
"+0000" it is not your localTime (CET) it is UTC time. Your timeZone is not stored with your NSDate object, only the corresponding UTC date and time for your localTime input.
let myDate = NSCalendar.currentCalendar().dateWithEra(1, year: 1984, month: 1, day: 7, hour: 0, minute: 0, second: 0, nanosecond: 0)!
myDate.descriptionWithLocale(NSLocale.currentLocale())! // "Saturday, January 7, 1984 at 12:00:00 AM Brasilia Standard Time"
NSTimeZone.localTimeZone().secondsFromGMT
Don't worry about the log output. It will be an unformatted date. The actual date seems to be exactly what you intended.
If you want to show the exact date, you have to display it using a NSDateFormatter.

Resources