Could you tell me how to change the time zone?

Asked 2 years ago, Updated 2 years ago, 44 views

The code below will save data that is out of time.I checked online and it seems that the time of Greenwich Observatory is recorded.Could you tell me how to change the time zone so that Japan time can be saved?

The environment is Swift4, MacBookPro.

Code for saving current time

import UIKit
import RealmSwift

class inputViewController:UIViewController {

@ IBAction func save(_sender:UIButton){
     try!realm.write{
        self.diary.title=self.titleTextField.text!
        self.diary.body=self.bodyTextView.text
        self.diary.date=NSDate()// Where is the problem?

Current Time Code

import UIKit
import RealmSwift

classViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...

// Cell Contents
    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
// Reusable Cells
        letcell: UITableViewCell= UITableViewCell (style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell")
// Set cell value
        let object = dataArray [indexPath.row]
        cell.textLabel?.text=object.title
        cell.detailTextLabel?.text=object.date.description//Problems
...
}

swift

2022-09-30 21:28

1 Answers

As mentioned in the comment, the NSDate in Apple's Foundation framework and the Date in its Swift version do not have a time zone.Typically, if the data type is NSDate or Date, the display should reflect the time zone.

NSDate seems to be converting to a display string only on this line.

cell.detailTextLabel?.text=object.date.description

Unfortunately, the conversion to a string in the description property does not reflect the time zone settings of the current device.(In other words, the behavior is not defined properly, so the display may be completely different depending on the version.)

Use DateFormatter to convert the NSDate (or Date) type into a display string.

class ViewController:UIViewController, UITableViewDelegate, UITableViewDataSource{

    //...

    // Create an instance of `DateFormatter` and keep it in properties
    let dateFormatter: DateFormatter={
        let df = DateFormatter()
        // Use the system standard format .medium to match various settings (including time zones) on the time zone)
        df.dateStyle=.medium
        df.timeStyle=.medium
        return df
    }()

    //...

    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        //...
        // Change the line cell.detailTextLabel?.text=object.date.description as follows:
        cell.detailTextLabel?.text=dateFormatter.string(from:object.date as Date)
        //...
    }

    //...
}

The above code shows the time zone set on the device, not "Japan time", but if you want to force it to be Japan time regardless of the device's current configuration, please specify a time zone.

let dateFormatter: DateFormatter={
    let df = DateFormatter()
    // To match the device language/12/24 hour display settings, but force only the time zone to Japan
    df.timeZone=TimeZone(identifier: "Asia/Tokyo")
    // Use .medium of the system standard format
    df.dateStyle=.medium
    df.timeStyle=.medium
    return df
}()

dateStyle, timeStyle displays a date and time that looks like an Apple built-in app, but on the other hand, if you don't like it and you want to specify a custom format, you'll be the code.

let dateFormatter: DateFormatter={
    let df = DateFormatter()
    // Make formatting unique to device time zone settings
    df.dateFormat="yyyy/MM/dd HH:mm:ss"
    df.locale=Locale(identifier: "en_US_POSIX")//<- To prevent other device settings from affecting formatting
    return df
}()

If you want to force a time zone, add one line to set the timeZone property as before.

DateFormattr has many other settings, but if you search around "ios dateformatter", you'll find a lot of good articles, including Apple's official reference, so look for the desired format.

Incidentally, the above method of "specifying a device time zone when viewing" may cause the display date and time to be different from what the user is aware of if the user moves and changes the device time zone after saving the date and time.

If you don't like this behavior, you might want to set the data type to String instead of using NSDate or Date to "convert it to a string representing the date (considering the time zone)."However, DateFormatter will also be used.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.