user2024080
user2024080

Reputation: 5101

How to convert the current date to digit format?

I am getting the new date like this:

getCalender: function () {

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

            var date = new Date();
            var day = date.getDate();
            var monthIndex = date.getMonth();
            var year = date.getFullYear();

            return (day + '-' + monthNames[monthIndex] + '-' + year);

        },

my above method returns the date format like "day" month name from array and the year.

But the back-end people requires the date format like this:

"IssueDate": "/Date(1407445200000+0300)/",

how to convert the date format to match this? Any one help me?

Upvotes: 1

Views: 193

Answers (3)

user2024080
user2024080

Reputation: 5101

I tried this, it works fine for me:

var date1 = new Date();
var n = date1.getTimezoneOffset();

console.log( '/Date(' + (+date1) + n +')/' );

Thanks everyone!

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

You can try like this:

var dt = new Date(myDate);
var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
return '/Date(' + newDate.getTime() + ')/';

DEMO

If you want to add the offset time then simply do

var dt = new Date();
var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
return '/Date(' + newDate.getTime()  +  newDate.getTimezoneOffset() +')/' ;

DEMO

Upvotes: 0

M3ghana
M3ghana

Reputation: 1281

Try using getTime() in jquery as below -

var date = new Date();

var time = date.getTime(); //1447995963967

Upvotes: 0

Related Questions