admin管理员组文章数量:1414857
How to extract only time from the date which is present in ISO format? I tried this:
var d = new Date('1970-01-15T03:32:12.000Z'); //ISO-8601 formatted date returned from server
console.log(d.getTime());// 1222332000
Expected op is : 03:32:12
How to extract only time from the date which is present in ISO format? I tried this:
var d = new Date('1970-01-15T03:32:12.000Z'); //ISO-8601 formatted date returned from server
console.log(d.getTime());// 1222332000
Expected op is : 03:32:12
7 Answers
Reset to default 6Since your server returns an ISO-8601 formatted date which has a predefined format, you can convert it to ISO string using toISOString()
and then get the substring of the time value:
var d = new Date('1970-01-15T03:32:12.000Z');
console.log(d.toISOString().substr(11,8));
Date.getTime()
returns the time in UNIX epoch format.
https://en.wikipedia/wiki/Unix_time
To access only the parameters you are interested in, you can use Date.getMinutes()
, Date.getMinutes()
, etc. See docs on MDN: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Note: Do not forget to spend one thought on time zones when you work with Date
's time, especially when your app runs in different regions.
You have to manually build the time string using Date.prototype
methods: getHours, getMinutes and getSeconds
Or use moment.js library.
Date.getTime()
gives you the unix timestamp, which is the number of seconds since january 1st 1970;
The getTime() method returns the numeric value corresponding to the time for the specified date according to universal time.
from MDN
You need to format the date yourself, either by concatenating the output of the Date.getHours()
, Date.getMinutes()
and Date.getSeconds()
methods, or by using one of the predefined formatting functions, like Date.toTimeString()
. Checkout the docs to pick your choice.
You can use getHours()
,getMinutes()
and getSecondes()
. Then you can use it with strings or objects.
Try the following:
d.toTimeString().split(' ')[0]
You can use moment.js to parse whatever format you like.
If you think moment.js is too big, there's another library call dayjs. The same fashion API but just 2KB. (Unfortunately, you can't do UTC time with dayjs yet.)
Update: Thanks kun for notifying the updates. You can now use UTC with dayjs plugin since v1.8.9.
var d = new Date('1970-01-15T03:32:12.000Z');
console.log(moment(d).utc().format('HH:mm:ss'));
dayjs.extend(dayjs_plugin_utc)
console.log(dayjs(d).utc().format('HH:mm:ss'));
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/dayjs/1.8.9/dayjs.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/dayjs/1.8.9/plugin/utc.js"></script>
本文标签: javascripthow to get only time from date in ISO formateStack Overflow
版权声明:本文标题:javascript - how to get only time from date in ISO formate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209904a2647819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论