最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get only time from date in ISO formate - Stack Overflow

programmeradmin1浏览0评论

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

Share Improve this question edited Aug 17, 2018 at 8:21 NoobTW 2,5742 gold badges26 silver badges46 bronze badges asked Aug 17, 2018 at 7:46 Trang DTrang D 3276 silver badges17 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 6

Since 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>

发布评论

评论列表(0)

  1. 暂无评论