0

I have downloaded a war file (not my code) websockets application for live chat(Java Application)

And this is the information i am receiving from back end (for which i don't have code)

Object {message: "as", sender: "sdas", received: "Thu Nov 19 21:12:36 IST 2015"}

Is it possible to change the date so that i can get only

Thu Nov 19 21:12:36 , instead of Thu Nov 19 21:12:36 IST 2015

function onMessageReceived(evt) {

    var msg = JSON.parse(evt.data); 
    console.log(msg);
    var $messageLine = $('<tr><td class="received">' + msg.received
            + '</td><td class="user label label-info">' + msg.sender
            + '</td><td class="message badge">' + msg.message
            + '</td></tr>');
    $chatWindow.append($messageLine);
}

Could you please let me know how to do this

1
  • Is the received a String or a Date? If Date you can easily change it, if String you can either parse it to however you need or convert to Date and change it Commented Nov 19, 2015 at 15:52

3 Answers 3

1

any way you are getting valid long date lets remove tailing two words of the string,

function simpleDateString(dt){       

    return dt.split(" ").splice(0, 4).join(" ");
}

use the above funtion to remove tailing words, lets modify your function

function onMessageReceived(evt) {

    var msg = JSON.parse(evt.data); 
    console.log(msg);
    var simpleDate = simpleDateString(msg.received); //parse your date string
    var $messageLine = $('<tr><td class="received">' + simpleDate 
            + '</td><td class="user label label-info">' + msg.sender
            + '</td><td class="message badge">' + msg.message
            + '</td></tr>');
    $chatWindow.append($messageLine);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use a regular expression to get everything before the time:

function onMessageReceived(evt) {
    var msg = JSON.parse(evt.data); 
    var date=msg.received.match(/(.*\d{1,2}:\d{2}:\d{2})/);
    var $messageLine = $('<tr><td class="received">' + date[1]
    ...

Comments

0

You can use Date:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

to automatically parse and create pretty much any format you want.

In addition, since it looks like your format is static, you can use simply string manipulation functions to extract only the main date (less versatile and generally less recommended, but more efficient and legacy-compatible).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.