使用lambda表达式将日期时间转换为MM Yyyy,DD[英] Convert datetime to MM yyyy, dd using lambda expression

本文是小编为大家收集整理的关于使用lambda表达式将日期时间转换为MM Yyyy,DD的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我试图将日期格式更改为MM:yyyy:dd(2018年10月24日),我尝试了我知道的每种方法,但它不起作用...这是我的代码(我在线的许多方法之一):

var data = (from o in db.tbPATxns
                            join p in db.providers on o.providerid equals p.providertin
                            join a in db.Endorsements on o.panumber equals a.panumber
                            join b in db.members on o.IID equals b.legacycode
                            join c in db.proceduredatas on a.proccode equals c.procedurecode
                            where c.proceduredesc.ToLower().Contains("admission")
                            select new
                            {
                                o.panumber,
                                b.legacycode,
                                b.lastname,
                                b.firstname,
                                b.phone1,
                                p.providername,
                                a.txndate.ToString("MMMM dd, yyyy")
                            });

                RadGrid1.DataSource = data.ToList();
                RadGrid1.DataBind();

请帮助我.

推荐答案

字母" mm"仅返回月号.如果要显示"月份"名称,则需要使用类似的内容:

DateTime.ToString("MMMM yyyy, dd", CultureInfo.InvariantCulture);

我认为您的代码是:

a.txndate.ToString("MMMM yyyy, dd", CultureInfo.InvariantCulture);

这将返回带有您想要的格式

的字符串

编辑:如果您想更改语言,则只需更改IformatProvider(CultureInfo.invariantCurlulture)

其他推荐答案

您可以使用 dateTime.tostring(string)指定DateTime格式.

MMMM全月名称(例如12月)
yyyy年,(例如2015年)
dd表示本月的一天为01至31.

var listDate = new List<DateTime>();
listDate.Add(new DateTime(1980, 5, 5));
listDate.Add(new DateTime(1982, 10, 20));
listDate.Add(new DateTime(1984, 1, 4));
listDate.Add(new DateTime(1979, 6, 19));

var result = listDate.Select(x => new { property1 = x.ToString("MMMM yyyy, dd"), property2 = x });

foreach (var d in result)
{
    Console.WriteLine(d.property1);
}

结果:

mai 1980, 05
octobre 1982, 20
janvier 1984, 04
juin 1979, 19

本文地址:https://www.itbaoku.cn/post/1557217.html

问题描述

I am trying to change the date format to MM:yyyy:dd (October 2018, 24), I've tried every method I know, but it's not working... here is my code(one of many method I got online):

var data = (from o in db.tbPATxns
                            join p in db.providers on o.providerid equals p.providertin
                            join a in db.Endorsements on o.panumber equals a.panumber
                            join b in db.members on o.IID equals b.legacycode
                            join c in db.proceduredatas on a.proccode equals c.procedurecode
                            where c.proceduredesc.ToLower().Contains("admission")
                            select new
                            {
                                o.panumber,
                                b.legacycode,
                                b.lastname,
                                b.firstname,
                                b.phone1,
                                p.providername,
                                a.txndate.ToString("MMMM dd, yyyy")
                            });

                RadGrid1.DataSource = data.ToList();
                RadGrid1.DataBind();

Please help me out.

推荐答案

The letters "MM" will return only the month number. If you want to show the month name you need to use something like this:

DateTime.ToString("MMMM yyyy, dd", CultureInfo.InvariantCulture);

I think in your code is:

a.txndate.ToString("MMMM yyyy, dd", CultureInfo.InvariantCulture);

This will return an string with the format you want


Edit: if you want to change de language you just need to change the IFormatProvider (CultureInfo.InvariantCulture)

其他推荐答案

You can use DateTime.ToString(String) to specify the DateTime format.

MMMM Full month name (e.g. December)
yyyy Year, (e.g. 2015)
dd Represents the day of the month as a number from 01 through 31.

var listDate = new List<DateTime>();
listDate.Add(new DateTime(1980, 5, 5));
listDate.Add(new DateTime(1982, 10, 20));
listDate.Add(new DateTime(1984, 1, 4));
listDate.Add(new DateTime(1979, 6, 19));

var result = listDate.Select(x => new { property1 = x.ToString("MMMM yyyy, dd"), property2 = x });

foreach (var d in result)
{
    Console.WriteLine(d.property1);
}

Result:

mai 1980, 05
octobre 1982, 20
janvier 1984, 04
juin 1979, 19