javascript jQuery 将日期时间转换为特定格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46542974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery converting datetime to specific format
提问by User987
I have a datetime which gets returned from server side in my .net application like following:
我的 .net 应用程序中有一个从服务器端返回的日期时间,如下所示:
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-ddformat?
如何将 jQuery/Javascript 中的时间转换为 yyyy-MM-dd格式?
Can someone help me out?
有人可以帮我吗?
P.S. Guys here is the query that returns the results and dates that I'm trying to convert:
PS Guys 这里是返回我试图转换的结果和日期的查询:
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.
附加信息:LINQ to Entities 无法识别方法 'System.String ToString(System.String)' 方法,并且此方法无法转换为存储表达式。
回答by Sagar
You can always use moment.js ( http://momentjs.com/)
您可以随时使用 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 您可以通过以下方式轻松隐藏:
moment(new Date()).format('yyyy-MM-dd');
回答by Tayyeb
moment(yourdate, 'DD/MM/YYYY HH:mm').format("DD/MM/YYYY HH:mm");
回答by Akshay
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
这是为你工作

