简体   繁体   English

使用date.js将ISO 8601转换为特定格式

[英]convert ISO 8601 to specific format using date.js

i have a ISO 8601 time in javascript not i want to convert it into MMM-D mm:ss using date.js but really no idea how can i accomplish it. 我在JavaScript中有ISO 8601 time ,不是我想使用date.js将其转换为MMM-D mm:ss但实际上不知道如何实现。

here is the sample date 2014-02-26T05:39:27.885Z i am getting this from server and if you convert it , it will appear like Wed, 26 Feb 2014 11:09:27 +05:30 and i want to show it as Feb - 26 09:27 这是示例日期2014-02-26T05:39:27.885Z我正在从服务器获取此信息,如果将其转换,它将显示为Wed, 26 Feb 2014 11:09:27 +05:30并且我想显示它是Feb - 26 09:27

using following i can i achieve this Feb - 26 09:27 使用以下我我可以实现这一目标Feb - 26 09:27

    var dateTime = Date.today().toString("MMM-d") +" "+ new Date().toString("mm:ss");

but this is for today's date i want it to fetch it from ISO 8601 但这是今天的日期,我希望它从ISO 8601获取它

Assuming date.js is loaded and you have the time as an ISO-8601 string, first create a Date object with new Date(str) then call .toString("MMM - d mm:ss") on it. 假设加载了date.js且您有时间使用ISO-8601字符串,请首先创建一个带有new Date(str)Date对象,然后在其上调用.toString("MMM - d mm:ss")

You can see this working in the below snippet. 您可以在下面的代码段中看到此功能。

 var dateStr = "2014-02-26T05:39:27.885Z"; var dateObj = new Date(dateStr); var formattedDate = dateObj.toString("MMM - d mm:ss"); console.log(formattedDate) // => "Feb - 25 39:27" 
 .as-console-wrapper{min-height:100%;} 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM