Reputation: 1302
var date = new Date();
it displays "Sun Jul 16 2017 00:05:50 GMT+0800 (SGT)"
But, I need only "Month date, year hour:minutes am". Please let me know to remove unwanted datas and display them.
Thanks
Upvotes: 2
Views: 2656
Reputation: 2516
You can modify this function according to your needs:
function formatJSDate()
{
return (dt.getMonth() +1 + '/' + dt.getDate() + '/' + dt.getYear() + ' ' + dt.toLocaleString('en-US', { hour: 'numeric',minute:'numeric', hour12: true }));
}
Now you can try this:
var dt = new Date('Sun Jul 16 2017 00:05:50 GMT+0800 (SGT)');
//or var dt= new Date();
alert(formatJSDate(dt));
OUTPUT:
7/15/117 9:05 AM
Upvotes: 0
Reputation: 15404
If you split the string, you can use the arrays splice fn to remove everything from 4 chars before 'GMT' on. Then just join back to a string:
var dateArray = date.split('');
dateArray.splice(date.indexOf('GMT') - 4);
var shorter = dateArray.join('');
Upvotes: 0
Reputation: 33933
I suggest you to use Moment.js for everything concerning dates...
;)
var date = moment().format("MMMM D, YYYY hh:mm A")
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Upvotes: 1