User987
User987

Reputation: 3825

jQuery converting datetime to specific format

I have a datetime which gets returned from server side in my .net application like following:

Jul 24 2017  7:33PM

I have tried to convert it into:

yyyy-MM-dd

format like following:

var date = new Date(data);
 var res = (date.getFullYear() + '-'(date.getMonth() + 1) + '-' + date.getDate());

But when I output the result in console the result I get is:

NaN-NaN-NaN

How can I convert the time in jQuery/Javascript into yyyy-MM-dd format?

Can someone help me out?

P.S. Guys here is the query that returns the results and dates that I'm trying to convert:

     var user = ctx.Users.Where(x => x.UserId == _parsedId)
                .Select(b => new
                {
                    Active = b.Active,
                    Email = b.Email,
                    Subscriptions = b.Subscriptions.Where(p => p.Active == true).Select(sub => new
                    {
                        SubscriptionID = sub.SubscriptionId,
                        Type = sub.SubTypes.SubName,
                        ReferenceID = sub.ReferenceId,
                        Active = sub.Active,
                        DateCreated = sub.CreateDate.ToString("yyyy-MM-dd"),
                        ExpirationDate = sub.ExpirationDate.ToString("yyyy-MM-dd")
                    }).ToList(),
                    Roles = b.UserRoles.Where(p => p.Active == true).ToList()
                })
                .AsEnumerable()
                .Select(x => x)
                .FirstOrDefault();


   // ExpirationDate - is of Type DateTime?
   // CreatedDate - is of Type DateTime 

And when I try to convert the datetime to specific format I get a following error:

Additional information: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

Upvotes: 2

Views: 36858

Answers (3)

Tayyeb
Tayyeb

Reputation: 147

moment(yourdate, 'DD/MM/YYYY HH:mm').format("DD/MM/YYYY HH:mm");

Upvotes: 2

Akshay
Akshay

Reputation: 1

you can change your server side code,

var user = ctx.Users.Where(x => x.UserId == _parsedId)
            .Select(b => new
            {
                Active = b.Active,
                Email = b.Email,
                Subscriptions = b.Subscriptions.Where(p => p.Active == true).ToList().Select(sub => new
                {
                    SubscriptionID = sub.SubscriptionId,
                    Type = sub.SubTypes.SubName,
                    ReferenceID = sub.ReferenceId,
                    Active = sub.Active,
                    DateCreated = sub.CreateDate.ToString("yyyy-MM-dd"),
                    ExpirationDate = sub.ExpirationDate.ToString("yyyy-MM-dd")
                }).ToList(),
                Roles = b.UserRoles.Where(p => p.Active == true).ToList()
            })
            .AsEnumerable()
            .Select(x => x)
            .FirstOrDefault();

this is work for you

Upvotes: 0

Sagar
Sagar

Reputation: 1424

You can always use moment.js ( http://momentjs.com/ )

It's one of the easiest methods to manipulate with Date and Time.

Using moment you can easily covert by :

moment(new Date()).format('yyyy-MM-dd');

Upvotes: 5

Related Questions