【问题标题】:how to convert a date in dd/mm/yyyy format to UTC format yyyy-MM-ddTHH:mm:ssZ如何将 dd/mm/yyyy 格式的日期转换为 UTC 格式 yyyy-MM-ddTHH:mm:ssZ
【发布时间】:2019-08-07 21:02:00
【问题描述】:

我想将 dd/mm/yyyy 格式的日期转换为 UTC 格式 yyyy-MM-ddTHH:mm:ssZ。谁能告诉我如何在 Angular 6 中做到这一点?

我尝试过使用 Date getUTCDate() 方法,但没有成功

this.date = new Date(newSearchData.beginDateUPD);
newSearchData.beginDateUPD = this.date.getUTCDate();

如果我的日期是 08/02/2019(即 2019 年 2 月 8 日),则输出应为 2019-02-07T18:30:00.000Z

【问题讨论】:

    标签: angular


    【解决方案1】:

    您可以通过提供输入格式和输出格式来完成此操作

    fun getMyDate(date: String?, outputFormat: String?, inputFormat: String?): String {
            try {
                val simpleDateFormat = SimpleDateFormat(inputFormat, Locale.getDefault())
                simpleDateFormat.timeZone = TimeZone.getTimeZone("UTC")
                return SimpleDateFormat(outputFormat, Locale(mAppLanguage)).format(simpleDateFormat.parse(date))
            } catch (e: ParseException) {
                e.printStackTrace()
            } catch (ex: NullPointerException) {
                ex.printStackTrace()
            }
            return ""
        }
    

    现在叫这个

    getMydate("YOUR DATE IN STRING","yyyy-MM-dd'T'HH:mm:ss.SSS'Z'","dd/MM/yyyy")
    

    如果需要,删除 UTC

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多