简体   繁体   English

在移动应用中包含moment.js

[英]Including moment.js in a mobile app

Is moment.js supposed to work on mobile platforms? Moment.js应该可以在移动平台上运行吗? I am building something in Phonegap and trying to get it to work on a page. 我在Phonegap中构建一些东西,并试图使其在页面上工作。

Here is what the start of my page looks like: 这是页面开头的样子:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<link rel="stylesheet" type="text/css" href="../css/jquery.mobile.flatui.css"/>
<script src="../js/jquery-1.10.2.min.js"></script>
<script src="../js/jquery.mobile-1.3.2.min.js"></script>
<script src="../js/moment.js"></script>
<script type="text/javascript">

I also configured requires.config to find it. 我还配置了require.config来查找它。 Not sure what is going wrong, or is there something I am completely oblivious to. 不知道出了什么问题,或者我完全忘了什么。 Any kind of pointers will be greatly appreciated. 任何种类的指针将不胜感激。

This is how I use it in my JS code: 这就是我在JS代码中使用它的方式:

var now = moment(); 
console.log("Playing with dates..."+now); 
console.log(moment().weekday(0));

But the calls don't seem to be going through. 但是电话似乎没有接通。

Yes, it should work in any javascript environment. 是的,它应该可以在任何 JavaScript环境中使用。

You just need to format your moment for display. 您只需要格式化显示时刻即可。 Something like this: 像这样:

var now = moment();
console.log("Playing with dates... " + now.format("YYYY-MM-DD h:mm:ss a");

Or whatever makes sense for how you want to display it. 或任何有意义的显示方式。 Refer to the momentjs documentation . 请参阅momentjs文档

Yes, it works for Mobile apps using Phonegap/Cordova. 是的,它适用于使用Phonegap / Cordova的移动应用程序。 It's a simple 2 step process as below ( Reference ); 这是一个简单的两步过程,如下所示( 参考 );

  1. Install/download the library 安装/下载库

Prompt$ npm install moment /Users/userName/Projects/Mobile/appName └── moment@2.18.1

  1. Start using it 开始使用它

var now = moment(); console.log( "Playing with dates..." + now ); console.log( moment().weekday(0) );

Probably your config has issues so it's a good idea to use it without Require.js initially. 可能您的配置有问题,因此最好在最初不使用Require.js的情况下使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM