2

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.

16
  • 2
    If possible, make the server output a better structured format such as the yyyy-MM-dd that you mentioned. Would save a lot of headaches on the client side. Commented Oct 3, 2017 at 11:00
  • 1
    new Date(Jul 24 2017 7:33PM); returns InvalidDate Commented Oct 3, 2017 at 11:02
  • "Note: Parsing of strings with Date.parse is strongly discouraged due to browser differences and inconsistencies." (Date.parse is what uses the constructor when string passed) - see here Commented Oct 3, 2017 at 11:02
  • @StephenMuecke A yes, I guess Peter made a good suggestion to change it in server side and avoid conversions in client side ... ? Commented Oct 3, 2017 at 11:03
  • Guys, I've pasted my server side code with conversion to specific format, but I'm getting an error here... Commented Oct 3, 2017 at 11:06

3 Answers 3

5

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Sagar, by doing this , for example input date: Jul 24 2017 7:33PM gets output like: 2017-10-03 , which isn't correct at all
Hello there, I tried and got the correct result. Can you please send the exact date so I can recheck.
2
moment(yourdate, 'DD/MM/YYYY HH:mm').format("DD/MM/YYYY HH:mm");

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.