# Calendar/DateComponents: Trying to find the first date in a week

**URL:** <https://forums.swift.org/t/calendar-datecomponents-trying-to-find-the-first-date-in-a-week/35937>\
**Category:** Using Swift\
**Tags:** date\
**Created:** [April 28, 2020, 10:41am UTC](https://forums.swift.org/t/calendar-datecomponents-trying-to-find-the-first-date-in-a-week/35937 "2020-04-28T10:41:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kai-vala](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@kai-vala](https://forums.swift.org/u/kai-vala)\
**Post date:** [April 28, 2020, 10:41am UTC](https://forums.swift.org/t/calendar-datecomponents-trying-to-find-the-first-date-in-a-week/35937/1 "2020-04-28T10:41:35Z")

</div>

Trying to find the fist date in the week of date 2020-04-17 (week 16).

I'm expecting the first day in the week to be 04-13 (a Monday) since I've set the firstWeekday as 2.

But the result date is 2020-04-12, which seems incorrect to me. Can anyone explain if I'm doing something wrong here, or have some incorrect understanding? Thanks very much.

Playground is here:

> <https://gist.github.com/kai-vala/5689c17cb72a00186e5c2fdcd563362d>

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [April 28, 2020, 1:03pm UTC](https://forums.swift.org/t/calendar-datecomponents-trying-to-find-the-first-date-in-a-week/35937/2 "2020-04-28T13:03:49Z")

</div>

> [@kai-vala](#):
>
> But the result date is 2020-04-12, which seems incorrect to me.

I believe the computation is correct, but you're confusing the computed date with the output printed to the console. On my computer, the output of the "wrong" print statement is:

```swift
Date: 2020-04-12 22:00:00 +0000

```

Note that a `Date` value has no time zone information. If you print a `Date` value to the console, Foundation will always print it in GMT. Since my time zone is 2 hours ahead of GMT, this date (April 12 at 22:00 GMT) is equal to April 13 at 00:00 in my time zone, so the computation is actually correct.

On your computer, the output might be different because your time zone might be different. If you use a fresh `DateFormatter` (which by default uses the current time zone/calendar) to print the date, you should see the date you expect.

Your assertion in the last line is failing because you're not taking the time zone into account. Your `expectedDate` value is set to April 13 at **00:00 GMT**. This does not correspond to April 13 at 00:00 in any other time zone.

```swift
let expectedDate = formatter.date(from: "2020-04-13 00:00:00 +0000")
XCTAssertEqual(firstDayInWeek, expectedDate, "Date did not match expected")

```

---

<div class="post-metadata">

**Author:** ![kai-vala](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@kai-vala](https://forums.swift.org/u/kai-vala)\
**Post date:** [April 28, 2020, 1:48pm UTC](https://forums.swift.org/t/calendar-datecomponents-trying-to-find-the-first-date-in-a-week/35937/3 "2020-04-28T13:48:32Z")

</div>

Thanks very much for your help, I see it now.

Updated gist as well:

> <https://gist.github.com/kai-vala/5689c17cb72a00186e5c2fdcd563362d/revisions>
