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

I would like to get the correct local time of the user on my website, but I noticed that the new Date c

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204988a4616550.html

相关推荐

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

    I would like to get the correct local time of the user on my website, but I noticed that the new Date c

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信