简体   繁体   中英

How to use moment js for displaying formatted date?

Guys I am getting date through ajax call in my ASP.net mvc application. 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:

  1. Like this 06/09/2014 05:38:55 PM
  2. Like three hours ago or Yesterday style (used by facebook)
  3. Just the date part like 06/09/2014

Basically I have a hidden input field in my view which gets the Date from ajax call. 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. Why is that? I think I was unable to follow the moment.js documentation. How can I use moment.js to get this formatted result ?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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