如何在C#中更改日期,年份的日期格式

时间:2017-11-29 04:44:50

标签: c#

我有一行代码。字符串类型的日期采用29/11/2017格式。         我希望它为November 29, 2017。          我尝试添加String.Format但它在pdf中显示相同的29/11/2017。

cellValDate.AddElement(new Phrase(String.Format("{0:dddd, MMMM d, yyyy}",
                       txtDate.Text, CultureInfo.CreateSpecificCulture("en-US")), 
                       new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 
                       10, iTextSharp.text.Font.NORMAL)));

1 个答案:

答案 0 :(得分:4)

首先,您需要解析<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv" xmlns="http://www.w3.org/1999/xhtml" xmlns:dm="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="array fn map math xhtml xs"> <xsl:output method="text" encoding="utf-8"/> <xsl:variable name="delimiter" select="','"/> <!-- xmlns:dm is the xmlns attribute in Data.--> <!-- define an array containing the fields we are interested in --> <xsl:variable name="fieldArray"> <field>Journal Articles</field> <field>Books</field> <field>Book Chapters</field> <field>Conference Proceeedings</field> <field>Others</field> </xsl:variable> <xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*" /> <xsl:key name="Year-Published" match="dm:INTELLCONT" use="dm:DTY_PUB"/> <xsl:template match="/dm:Data"> <!--key("Year-Published", '2017')--> <!--<xsl:text>Journal Articles</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Books</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Book Chapters</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Conference Proceedings</xsl:text> <xsl:value-of select="$delimiter" /> <xsl:text>Others</xsl:text> --> <xsl:text>Year,</xsl:text> <!-- tabulating Years as columns --> <xsl:for-each select="dm:Record/dm:INTELLCONT[generate-id()=generate-id(key('Year-Published', dm:DTY_PUB)[1])]"> <xsl:sort select="(dm:DTY_PUB)" order="ascending"/> <xsl:value-of select="(dm:DTY_PUB)"/> <xsl:text>,</xsl:text> </xsl:for-each> <xsl:text>&#xa;</xsl:text> <!-- Tabulating Published Papers Per Year--> <xsl:for-each select="dm:Record/dm:INTELLCONT[generate-id()=generate-id(key('Year-Published', dm:DTY_PUB)[1])]"> <xsl:sort select="(dm:DTY_PUB)" order="ascending"/> <xsl:value-of select="count(dm:Record/dm:INTELLCONT[dm:CONTYPE='Journal Article'][dm:STATUS='Published'][dm:DTY_PUB=key('Year-Published', dm:DTY_PUB)[1])][dm:USER_REFERENCE_CREATOR!='No'])"/> <xsl:value-of select="$delimiter" /> </xsl:for-each> <xsl:text>&#xa;</xsl:text> <!-- output newline --> <xsl:text>&#xa;</xsl:text> </xsl:template> </xsl:stylesheet>中的日期字符串值并将其转换为txtDate.Text对象。

DateTime

然后将其转换为特定格式 - &#34;月日,年&#34;。

DateTime dt = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

然后你可以把它传递给你的代码 -

string formateDate = dt.ToString("MMMM dd, yyyy");