IOS Swift-da NSDate-ni stringga aylantiring

NSDate kodini String ga o'zgartiraman va keyin formatni o'zgartiraman. Lekin NSDate kodidan String ga o'tib, u bo'shliqni ishlab chiqaradi.

 let formatter = DateFormatter()
 let myString = (String(describing: date))
 formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
 let yourDate: Date? = formatter.date(from: myString)
 formatter.dateFormat = "dd-MMM-yyyy"
 print(yourDate)
32
ur sana ni ko'rsatish, u joriy sana yoki boshqa
qo'shib qo'ydi muallif Anbu.karthik, manba
Datepicker natijasini ko'rsatish
qo'shib qo'ydi muallif Anbu.karthik, manba
uning joriy emasligi Sana, men Datepicker'dan olaman
qo'shib qo'ydi muallif iOS, manba

7 javoblar

batafsil ma'lumotni Apple Dateformatter Document dan olishingiz mumkin .Agar sizga format ma'lumotlarini dateString-ga qarang. havolasini , batafsil ma'lumotni bu yerda Masalan, shunga o'xshash narsa

let formatter = DateFormatter()
// initially set the format based on your datepicker date/server String
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let myString = formatter.string(from: Date())//string purpose I add here 
// convert your string to date
let yourDate = formatter.date(from: myString)
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd-MMM-yyyy"
// again convert your date to string
let myStringafd = formatter.string(from: yourDate!)

print(myStringafd)

chiqdi sifatida qabul qilasiz

enter image description here

91
qo'shib qo'ydi
ammo tarixning bizni qayta ko'rib chiqishga asoslangan holda aylantirishimiz kerak bo'lgan formati yo'q
qo'shib qo'ydi muallif Anbu.karthik, manba
Rahmat Anbu ishlaydi
qo'shib qo'ydi muallif iOS, manba

Sana kodini String ga aylantirishda doimo ushbu koddan foydalanaman. (Swift 3)

extension Date
{
    func toString( dateFormat format  : String ) -> String
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self)
    }

}

va shunga o'xshash qo'ng'iroq qiling. .

let today = Date()
today.toString(dateFormat: "dd-MM")
32
qo'shib qo'ydi

Yangilangan kodingizni yangilang.

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: date as Date)
let yourDate: Date? = formatter.date(from: myString)
formatter.dateFormat = "dd-MMM-yyyy"
print(yourDate!)
4
qo'shib qo'ydi
ammo sizning Daty ma'lumotlar turi ma'lumotidan
qo'shib qo'ydi muallif Aviram Netanel, manba

DateFormatter has some factory date styles for those too lazy to tinker with formatting strings. If you don't need a custom style, here's another option:

extension Date {  
  func asString(style: DateFormatter.Style) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = style
    return dateFormatter.string(from: self)
  }
}

Bu sizga quyidagi uslublarni beradi:

short, medium, long, full

Misoldan foydalanish:

let myDate = Date()
myDate.asString(style: .full)  //Wednesday, January 10, 2018
myDate.asString(style: .long)  //January 10, 2018
myDate.asString(style: .medium)//Jan 10, 2018
myDate.asString(style: .short) //1/10/18
2
qo'shib qo'ydi

Formatlashni yaratishda yodda tutish kerak bo'lgan narsa, agar siz formatterlar yaratish uchun juda qimmat hisoblangan bo'lsa, xuddi shunday misolni qayta ishlatishga harakat qilishdir. Quyidagilardan farqli o'laroq, dasturchilar uchun tez-tez foydalanadigan ilovalar uchun ishlatiladigan noutbordim: NSHipster'dan moslashtirilgan dasturlashtiruvchisi >.

extension DateFormatter {

    static var sharedDateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()   
       //Add your formatter configuration here     
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dateFormatter
    }()
}

Foydalanish:

let dateString = DateFormatter.sharedDateFormatter.string(from: Date())

1
qo'shib qo'ydi

Ushbu kengaytmani foydalanishingiz mumkin:

extension Date {

    func toString(withFormat format: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        let myString = formatter.string(from: self)
        let yourDate = formatter.date(from: myString)
        formatter.dateFormat = format

        return formatter.string(from: yourDate!)
    }
}

And use it in your view controller like this (replace <"yyyy"> with your format):

yourString = yourDate.toString(withFormat: "yyyy")
1
qo'shib qo'ydi

DateFormatter ajratib bo'lgandan keyin siz formatlangan mag'lubiyatni berishingiz kerak u holda bu kabi mag'lubiyatga aylantira olasiz

var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: date)
let yourDate: Date? = formatter.date(from: myString)
formatter.dateFormat = "dd-MMM-yyyy"
let updatedString = formatter.string(from: yourDate!)
print(updatedString)

OutPut

01-mart-2017

0
qo'shib qo'ydi