Convert 12Hr UTC date to 24Hr Local TimeZone format in Swift












1














As per UTC to IST conversion tool if UTC 12Hr date string is = "2018-12-31 07:35 PM" then it needs to be converted as "2019-01-01 01:05" (24Hr format in IST) [note : here 12Hr IST converted date is "2019-01-01 01:05 AM"]



So how do to convert this 12Hr UTC date into 24Hr IST dateFormat?



current code is :



static func convertUTCdateToLocalTimezoneDate(dateString: String, fromDateFormat: String, toDateFormat: String) -> String {
if dateString.count > 0 {
let dateFormatter = DateFormatter()
let is24Hr: Bool = AppUtility.isSystemTimeFormatIs24Hr(dateString: AppUtility.getIphoneCurrnetTime())
let fromDateFormatLocal = is24Hr == true ? kDateIn24Format : kDateIn12Format
dateFormatter.dateFormat = fromDateFormatLocal
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let date = dateFormatter.date(from: dateString)
var timeStamp = ""
dateFormatter.dateFormat = toDateFormat
dateFormatter.timeZone = NSTimeZone.local
timeStamp = dateFormatter.string(from: date!)
return timeStamp
}
return ""
}

static func isSystemTimeFormatIs24Hr(dateString: String) -> Bool {
let formatter = DateFormatter()
formatter.locale = NSLocale.current
formatter.dateStyle = .none
formatter.timeStyle = .short
let amRange: NSRange? = (dateString as NSString).range(of: formatter.amSymbol)
let pmRange: NSRange? = (dateString as NSString).range(of: formatter.pmSymbol)
let is24h: Bool = amRange?.location == NSNotFound && pmRange?.location == NSNotFound
return is24h
}









share|improve this question





























    1














    As per UTC to IST conversion tool if UTC 12Hr date string is = "2018-12-31 07:35 PM" then it needs to be converted as "2019-01-01 01:05" (24Hr format in IST) [note : here 12Hr IST converted date is "2019-01-01 01:05 AM"]



    So how do to convert this 12Hr UTC date into 24Hr IST dateFormat?



    current code is :



    static func convertUTCdateToLocalTimezoneDate(dateString: String, fromDateFormat: String, toDateFormat: String) -> String {
    if dateString.count > 0 {
    let dateFormatter = DateFormatter()
    let is24Hr: Bool = AppUtility.isSystemTimeFormatIs24Hr(dateString: AppUtility.getIphoneCurrnetTime())
    let fromDateFormatLocal = is24Hr == true ? kDateIn24Format : kDateIn12Format
    dateFormatter.dateFormat = fromDateFormatLocal
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    let date = dateFormatter.date(from: dateString)
    var timeStamp = ""
    dateFormatter.dateFormat = toDateFormat
    dateFormatter.timeZone = NSTimeZone.local
    timeStamp = dateFormatter.string(from: date!)
    return timeStamp
    }
    return ""
    }

    static func isSystemTimeFormatIs24Hr(dateString: String) -> Bool {
    let formatter = DateFormatter()
    formatter.locale = NSLocale.current
    formatter.dateStyle = .none
    formatter.timeStyle = .short
    let amRange: NSRange? = (dateString as NSString).range(of: formatter.amSymbol)
    let pmRange: NSRange? = (dateString as NSString).range(of: formatter.pmSymbol)
    let is24h: Bool = amRange?.location == NSNotFound && pmRange?.location == NSNotFound
    return is24h
    }









    share|improve this question



























      1












      1








      1


      0





      As per UTC to IST conversion tool if UTC 12Hr date string is = "2018-12-31 07:35 PM" then it needs to be converted as "2019-01-01 01:05" (24Hr format in IST) [note : here 12Hr IST converted date is "2019-01-01 01:05 AM"]



      So how do to convert this 12Hr UTC date into 24Hr IST dateFormat?



      current code is :



      static func convertUTCdateToLocalTimezoneDate(dateString: String, fromDateFormat: String, toDateFormat: String) -> String {
      if dateString.count > 0 {
      let dateFormatter = DateFormatter()
      let is24Hr: Bool = AppUtility.isSystemTimeFormatIs24Hr(dateString: AppUtility.getIphoneCurrnetTime())
      let fromDateFormatLocal = is24Hr == true ? kDateIn24Format : kDateIn12Format
      dateFormatter.dateFormat = fromDateFormatLocal
      dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
      let date = dateFormatter.date(from: dateString)
      var timeStamp = ""
      dateFormatter.dateFormat = toDateFormat
      dateFormatter.timeZone = NSTimeZone.local
      timeStamp = dateFormatter.string(from: date!)
      return timeStamp
      }
      return ""
      }

      static func isSystemTimeFormatIs24Hr(dateString: String) -> Bool {
      let formatter = DateFormatter()
      formatter.locale = NSLocale.current
      formatter.dateStyle = .none
      formatter.timeStyle = .short
      let amRange: NSRange? = (dateString as NSString).range(of: formatter.amSymbol)
      let pmRange: NSRange? = (dateString as NSString).range(of: formatter.pmSymbol)
      let is24h: Bool = amRange?.location == NSNotFound && pmRange?.location == NSNotFound
      return is24h
      }









      share|improve this question















      As per UTC to IST conversion tool if UTC 12Hr date string is = "2018-12-31 07:35 PM" then it needs to be converted as "2019-01-01 01:05" (24Hr format in IST) [note : here 12Hr IST converted date is "2019-01-01 01:05 AM"]



      So how do to convert this 12Hr UTC date into 24Hr IST dateFormat?



      current code is :



      static func convertUTCdateToLocalTimezoneDate(dateString: String, fromDateFormat: String, toDateFormat: String) -> String {
      if dateString.count > 0 {
      let dateFormatter = DateFormatter()
      let is24Hr: Bool = AppUtility.isSystemTimeFormatIs24Hr(dateString: AppUtility.getIphoneCurrnetTime())
      let fromDateFormatLocal = is24Hr == true ? kDateIn24Format : kDateIn12Format
      dateFormatter.dateFormat = fromDateFormatLocal
      dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
      let date = dateFormatter.date(from: dateString)
      var timeStamp = ""
      dateFormatter.dateFormat = toDateFormat
      dateFormatter.timeZone = NSTimeZone.local
      timeStamp = dateFormatter.string(from: date!)
      return timeStamp
      }
      return ""
      }

      static func isSystemTimeFormatIs24Hr(dateString: String) -> Bool {
      let formatter = DateFormatter()
      formatter.locale = NSLocale.current
      formatter.dateStyle = .none
      formatter.timeStyle = .short
      let amRange: NSRange? = (dateString as NSString).range(of: formatter.amSymbol)
      let pmRange: NSRange? = (dateString as NSString).range(of: formatter.pmSymbol)
      let is24h: Bool = amRange?.location == NSNotFound && pmRange?.location == NSNotFound
      return is24h
      }






      ios swift date timezone nsdateformatter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 at 4:44

























      asked Nov 20 at 10:46









      SuvaP

      224




      224
























          1 Answer
          1






          active

          oldest

          votes


















          -1














          // let string12H = "2018-11-30 07:35 PM"
          // let string24H = "2018-11-30 17:35:22"

          // let date12H = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST")
          // let date24H = getDateFrom(string24H, withFormat: "yyyy-MM-dd HH:mm:ss", timeZone: "IST")


          func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC") -> Date? {
          var dateToReturn: Date?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: "en_US_POSIX")
          formatter.dateFormat = formatString
          dateToReturn = formatter.date(from: dateString)
          return dateToReturn
          }

          func getStringFrom(_ date: Date, withFormat formatString: String) -> String? {
          var stringToReturn: String?
          let formatter = DateFormatter()
          formatter.dateFormat = formatString
          formatter.locale = Locale(identifier: "en_US_POSIX")
          stringToReturn = formatter.string(from: date)

          return stringToReturn
          }





          share|improve this answer























          • @SuvaP is your issue resolved?
            – iOS_Developer
            Nov 20 at 18:56










          • let me know if my answer needs improvement.
            – iOS_Developer
            Nov 20 at 19:02










          • Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
            – SuvaP
            Nov 23 at 4:13












          • I have updated commented section with examples
            – iOS_Developer
            Nov 23 at 7:01










          • HH:mm:ss is used for 24 hour format
            – iOS_Developer
            Nov 23 at 7:01











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391302%2fconvert-12hr-utc-date-to-24hr-local-timezone-format-in-swift%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          -1














          // let string12H = "2018-11-30 07:35 PM"
          // let string24H = "2018-11-30 17:35:22"

          // let date12H = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST")
          // let date24H = getDateFrom(string24H, withFormat: "yyyy-MM-dd HH:mm:ss", timeZone: "IST")


          func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC") -> Date? {
          var dateToReturn: Date?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: "en_US_POSIX")
          formatter.dateFormat = formatString
          dateToReturn = formatter.date(from: dateString)
          return dateToReturn
          }

          func getStringFrom(_ date: Date, withFormat formatString: String) -> String? {
          var stringToReturn: String?
          let formatter = DateFormatter()
          formatter.dateFormat = formatString
          formatter.locale = Locale(identifier: "en_US_POSIX")
          stringToReturn = formatter.string(from: date)

          return stringToReturn
          }





          share|improve this answer























          • @SuvaP is your issue resolved?
            – iOS_Developer
            Nov 20 at 18:56










          • let me know if my answer needs improvement.
            – iOS_Developer
            Nov 20 at 19:02










          • Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
            – SuvaP
            Nov 23 at 4:13












          • I have updated commented section with examples
            – iOS_Developer
            Nov 23 at 7:01










          • HH:mm:ss is used for 24 hour format
            – iOS_Developer
            Nov 23 at 7:01
















          -1














          // let string12H = "2018-11-30 07:35 PM"
          // let string24H = "2018-11-30 17:35:22"

          // let date12H = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST")
          // let date24H = getDateFrom(string24H, withFormat: "yyyy-MM-dd HH:mm:ss", timeZone: "IST")


          func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC") -> Date? {
          var dateToReturn: Date?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: "en_US_POSIX")
          formatter.dateFormat = formatString
          dateToReturn = formatter.date(from: dateString)
          return dateToReturn
          }

          func getStringFrom(_ date: Date, withFormat formatString: String) -> String? {
          var stringToReturn: String?
          let formatter = DateFormatter()
          formatter.dateFormat = formatString
          formatter.locale = Locale(identifier: "en_US_POSIX")
          stringToReturn = formatter.string(from: date)

          return stringToReturn
          }





          share|improve this answer























          • @SuvaP is your issue resolved?
            – iOS_Developer
            Nov 20 at 18:56










          • let me know if my answer needs improvement.
            – iOS_Developer
            Nov 20 at 19:02










          • Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
            – SuvaP
            Nov 23 at 4:13












          • I have updated commented section with examples
            – iOS_Developer
            Nov 23 at 7:01










          • HH:mm:ss is used for 24 hour format
            – iOS_Developer
            Nov 23 at 7:01














          -1












          -1








          -1






          // let string12H = "2018-11-30 07:35 PM"
          // let string24H = "2018-11-30 17:35:22"

          // let date12H = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST")
          // let date24H = getDateFrom(string24H, withFormat: "yyyy-MM-dd HH:mm:ss", timeZone: "IST")


          func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC") -> Date? {
          var dateToReturn: Date?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: "en_US_POSIX")
          formatter.dateFormat = formatString
          dateToReturn = formatter.date(from: dateString)
          return dateToReturn
          }

          func getStringFrom(_ date: Date, withFormat formatString: String) -> String? {
          var stringToReturn: String?
          let formatter = DateFormatter()
          formatter.dateFormat = formatString
          formatter.locale = Locale(identifier: "en_US_POSIX")
          stringToReturn = formatter.string(from: date)

          return stringToReturn
          }





          share|improve this answer














          // let string12H = "2018-11-30 07:35 PM"
          // let string24H = "2018-11-30 17:35:22"

          // let date12H = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST")
          // let date24H = getDateFrom(string24H, withFormat: "yyyy-MM-dd HH:mm:ss", timeZone: "IST")


          func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC") -> Date? {
          var dateToReturn: Date?
          let formatter = DateFormatter()
          formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
          formatter.locale = Locale(identifier: "en_US_POSIX")
          formatter.dateFormat = formatString
          dateToReturn = formatter.date(from: dateString)
          return dateToReturn
          }

          func getStringFrom(_ date: Date, withFormat formatString: String) -> String? {
          var stringToReturn: String?
          let formatter = DateFormatter()
          formatter.dateFormat = formatString
          formatter.locale = Locale(identifier: "en_US_POSIX")
          stringToReturn = formatter.string(from: date)

          return stringToReturn
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 at 6:58

























          answered Nov 20 at 12:46









          iOS_Developer

          1821315




          1821315












          • @SuvaP is your issue resolved?
            – iOS_Developer
            Nov 20 at 18:56










          • let me know if my answer needs improvement.
            – iOS_Developer
            Nov 20 at 19:02










          • Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
            – SuvaP
            Nov 23 at 4:13












          • I have updated commented section with examples
            – iOS_Developer
            Nov 23 at 7:01










          • HH:mm:ss is used for 24 hour format
            – iOS_Developer
            Nov 23 at 7:01


















          • @SuvaP is your issue resolved?
            – iOS_Developer
            Nov 20 at 18:56










          • let me know if my answer needs improvement.
            – iOS_Developer
            Nov 20 at 19:02










          • Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
            – SuvaP
            Nov 23 at 4:13












          • I have updated commented section with examples
            – iOS_Developer
            Nov 23 at 7:01










          • HH:mm:ss is used for 24 hour format
            – iOS_Developer
            Nov 23 at 7:01
















          @SuvaP is your issue resolved?
          – iOS_Developer
          Nov 20 at 18:56




          @SuvaP is your issue resolved?
          – iOS_Developer
          Nov 20 at 18:56












          let me know if my answer needs improvement.
          – iOS_Developer
          Nov 20 at 19:02




          let me know if my answer needs improvement.
          – iOS_Developer
          Nov 20 at 19:02












          Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
          – SuvaP
          Nov 23 at 4:13






          Provided Solution is not working for 24Hr TimeZone. The input date string is 12Hr UTC and we need to convert it into 24Hr Local TimeZone Format.
          – SuvaP
          Nov 23 at 4:13














          I have updated commented section with examples
          – iOS_Developer
          Nov 23 at 7:01




          I have updated commented section with examples
          – iOS_Developer
          Nov 23 at 7:01












          HH:mm:ss is used for 24 hour format
          – iOS_Developer
          Nov 23 at 7:01




          HH:mm:ss is used for 24 hour format
          – iOS_Developer
          Nov 23 at 7:01


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391302%2fconvert-12hr-utc-date-to-24hr-local-timezone-format-in-swift%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks