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?
- What are the errors you're getting? – Jonathan Rys Commented May 12, 2018 at 0:47
2 Answers
Reset to default 3You 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
评论列表(0条)