prajeesh
prajeesh

Reputation: 2382

Convert DateTime to specific format - Javascript

I have the following code

new Date(1501796503599)

which returns the value

Fri Aug 04 2017 03:11:43 GMT+0530 (IST)

I need the above value to be displayed as "Today, 3:11 am". Is there a way to do this

Upvotes: 1

Views: 52

Answers (4)

Soft One Global
Soft One Global

Reputation: 175

var monthName = ["January", "February", "March", "April", "May", "June",
               "July", "August", "September", "October", "November", "December"];


$(function(){
        var newDate = '';

            newDate = new Date(1501796503599)
        if (newDate != '')
            var str = newDate.getDate() + "-" + monthName[newDate.getMonth()] + "-" + newDate.getFullYear()+
           " " + newDate.getHours() + ":" + newDate.getMinutes() + ":" + newDate.getSeconds();

        console.log(str);

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

try this one.

Upvotes: 0

Sanil
Sanil

Reputation: 126

you can simply check using javascript without any dependencies.

var yourDate = new Date(1501796503599); 
var today = new Date(); 
var isToday = (today.toDateString() == yourDate.toDateString()); 
if(isToday){
    yourDate = "Today, " + yourDate.toLocaleTimeString();
}
else{
  yourDate;
}

Upvotes: 0

prajeesh
prajeesh

Reputation: 2382

var time = moment(1501796503599).calendar();
console.log(time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>

Upvotes: 2

Amiga500
Amiga500

Reputation: 6131

Moment.js should be suitable for your needs. Have a look at their documentation.

This should work for you:

moment().calendar(); 

Upvotes: 1

Related Questions