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

javascript - How do I import moment.js? - Stack Overflow

programmeradmin0浏览0评论

I would like to get the correct local time of the user on my website, but I noticed that the new Date constructor does not account for the timezone. I discovered Moment.js, which allows me to get the correct local time in the browser development console when i issue moment().format();, but not in my actual JavaScript file.

I have included the Moment.js file in my HTML document <script src="js/moment-with-locales.js"></script>, but have not imported it in my JavaScript file. How do i import the Moment.js functionality into my JavaScript file so that I can run console.log(moment().format()); without an error?

I would like to get the correct local time of the user on my website, but I noticed that the new Date constructor does not account for the timezone. I discovered Moment.js, which allows me to get the correct local time in the browser development console when i issue moment().format();, but not in my actual JavaScript file.

I have included the Moment.js file in my HTML document <script src="js/moment-with-locales.js"></script>, but have not imported it in my JavaScript file. How do i import the Moment.js functionality into my JavaScript file so that I can run console.log(moment().format()); without an error?

Share Improve this question asked May 12, 2018 at 0:41 user9621840user9621840 1
  • What are the errors you're getting? – Jonathan Rys Commented May 12, 2018 at 0:47
Add a ment  | 

2 Answers 2

Reset to default 3

You just need to load the script before your script in the html like this:

<head>
    <!-- The momentjs library -->
    <script src="js/moment-with-locales.js"></script>
    <!-- Your javascript -->
    <script src="js/my-custom-script.js"></script>
</head>

Then in your script you can use momentjs.

moment.utc('2018-01-01 23:45:55').local().format()

If you have to use it directly in your HTML file, do

<script>
moment().format();
</script>

If you have to use it in a different javascript file (rendered on the browser as a script src in the app HTML), you can use include it using webpack.

Thanks Sriram

发布评论

评论列表(0)

  1. 暂无评论