Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    194
    Thanks
    50
    Thanked 0 Times in 0 Posts

    Trouble getting month name from timestamp

    Hi All,

    I have been trying to get the month name from a timestamp formatted as follows:

    example:
    2014-05-05 15:38:49

    Here is what I have. "rows" is the records being passed to the function):

    Code:
    "Month Name": function(rows) {
      var date = rows["tStamp"];
      var monthName = date.getMonth();
      return monthName;
    }
    I am a novice with jQuery and don't have my chops yet but I appreciate any guidance.

    Thanks in advance.

    Ken

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    javascript won't give you the name - you have to make an array and use the index it gives you. You could do it like this:
    Code:
    var mths=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
    var datestr = "2014-05-05 15:38:49";
    var date = new Date(datestr);
    var monthNum = date.getMonth(); 
    alert(mths[monthNum]);
    but if you know the timestamp will always be in that format, you could just as easily do this:
    Code:
    var mths=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
    var datestr = "2014-05-05 15:38:49";
    var monthNum = Number(datestr.split("-")[1]);
    alert(mths[monthNum-1]);

  • Users who have thanked xelawho for this post:

    MaDmiX (08-06-2014)

  • #3
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    194
    Thanks
    50
    Thanked 0 Times in 0 Posts
    Thanks xelawho,

    This worked:
    Code:
    "Month Name": function(rows) {
      var mths=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
      var datestr = rows["tStamp"];
      var monthNum = Number(datestr.split("-")[1]);
      return mths[monthNum-1];
    }
    Unfortunately the months don't sort properly now, as they want to sort alphabetically

    This works to get the sort working properly:
    Code:
    "Month Name": function(rows) {
      var mths=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
      var datestr = rows["tStamp"];
      var monthNum = Number(datestr.split("-")[1]);
      return monthNum+mths[monthNum-1];
    }
    Not as pretty as I would like but at least it works

    Ken
    Last edited by MaDmiX; 08-06-2014 at 11:12 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •