简体   繁体   English

如何使用Moment JS显示格式化日期?

[英]How to use moment js for displaying formatted date?

Guys I am getting date through ajax call in my ASP.net mvc application. 伙计们,我通过ASP.net mvc应用程序中的ajax调用获取日期。 Now the date I am getting is in the format of 2014-06-09T17:38:55 however I want to display it in three formats: 现在我得到的日期是2014-06-09T17:38:55的格式,但是我想以三种格式显示它:

  1. Like this 06/09/2014 05:38:55 PM 赞了06/09/2014 05:38:55 PM
  2. Like three hours ago or Yesterday style (used by facebook) three hours agoYesterday风格(由Facebook使用)
  3. Just the date part like 06/09/2014 只是日期部分,如06/09/2014

Basically I have a hidden input field in my view which gets the Date from ajax call. 基本上,我的视图中有一个隐藏的输入字段,可从ajax调用获取日期。 Now I am doing something like this : 现在我正在做这样的事情:

<input id="hiddenLastAlertDate" type="hidden" value="values.CreationDate" />

In my script section, I have a function in which I am doing something like this: 在我的脚本部分,我有一个函数在执行以下操作:

var datefield = document.getElementById("hiddenLastAlertDate").value;
formattedDate = moment(datefield).format('L'); //for getting the date part only

Upon running the application,formattedDate shows up as undefined. 运行该应用程序后,formattedDate显示为未定义。 Why is that? 这是为什么? I think I was unable to follow the moment.js documentation. 我认为我无法遵循moment.js文档。 How can I use moment.js to get this formatted result ? 我如何使用moment.js来获取格式化结果?

moment("20140618 08:00", "YYYYMMDD hh:mm").fromNow(); will return 2 hours ago 将在2 hours ago返回

moment("20140617", "YYYYMMDD").fromNow(); will return a day ago a day ago返回

moment().format('DD/MM/YYYY h:mm:ss A'); will return 18/06/2014 9:42:24 AM 会在18/06/2014 9:42:24 AM返回18/06/2014 9:42:24 AM

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

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