TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Project 4: BetterRest - Date picker not formatted the same as the instructor's version

Forums > 100 Days of SwiftUI

When I create the same code as the instructor I get this format with day, month and year:

Wrong Format

However, the instructor with the same exact code gets the month, day and year with a comma between month and year. How do I fix this?

Correct Format

Code:

//
//  ContentView.swift
//  BetterRest
//
//

import SwiftUI

struct ContentView: View
{
    @State private var wakeUp = Date.now

    var body: some View
    {
        DatePicker("Please enter a date", selection: $wakeUp)
            .labelsHidden()
    }
}

struct ContentView_Previews: PreviewProvider
{
    static var previews: some View
    {
        ContentView()
    }
}

2      

Hi! This just because the first screenshot has different Locale than the second one. In the last one it is US Locale, so that it why you have month first and day second. It depends on settings of your Language and Region.

2      

Hi! This just because the first screenshot has different Locale than the second one. In the last one it is US Locale, so that it why you have month first and day second. It depends on settings of your Language and Region.

So how do I fix this? Do I change the region and locale of my Mac or Xcode? How do I do this? Can I fix it in code using swiftui?

2      

On your simulator you can change Region to US and it will be exactly the same as on the first screen.

3      

Here is the language and region on my Mac:

Language and region on mac

2      

Cool thanks that work and you can also change the locale of the preview as well. Here are my instructions:

Change system locale on iOS simulator and xcode preview to get the right date format

  1. On iOS simulator go to settings and general and change language and region to US for the simulator to work.
  2. For the preview go in xcode to product > scheme > edit scheme and change app region to US under options.

2      

or add this to DatePicker

.environment(\.locale, Locale(identifier: "en_US"))

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.