Reputation: 1287
I have written the below query to create an xml from entity, I need to get date time in .NET in yyyymmddhhmmss
format for the field SLOTTINGTIME
, so I have thought of writing a new method to get date time in desired format.
var slottingmessagexml = new XDocument(new XElement("Message",
new XAttribute("ID","SLT"),
new XElement("Record",
new XAttribute("STORENO",slottingmessage.StoreID),
new XAttribute("PICKLOCATION",slottingmessage.PickLocation),
new XAttribute("TPNB",slottingmessage.ProductID),
new XAttribute("SLOTTINGTIME",GetDateTimeInNewFormat(slottingmessage.SlottingDateTime)),
new XAttribute("SLOTTTINGACTION",slottingmessage.SlottingAction))
)
);
Upvotes: 2
Views: 30373
Reputation: 223187
You can use
string strDate = DateTime.Now.ToString("yyyyMMddhhmmss");
If 24hr format is required that use uppercase HH
inplace of hh
in the format string.
Remember the first MM
should be in upper case as lower case mm
is for minutes where as uppercase is for Month.
For your particular case instead of writing a new method you can do:
new XAttribute("SLOTTINGTIME",slottingmessage.SlottingDateTime.ToString("yyyyMMddhhmmss")),
One more thing to add: The output will contain Hour in 12 hours format because of the lower case hh
part in the string. Not really sure if you need that because without AM/PM this can't indicate the accurate time. For that purpose use HH
for hours which will display hours in 24 hour format. So your code could be:
new XAttribute("SLOTTINGTIME",slottingmessage.SlottingDateTime.ToString("yyyyMMddHHmmss")),
//^^ for 24 hours format
Upvotes: 16
Reputation: 48415
Although your question does not explicitly say what format you are looking for, I think that based on your example format of yyyymmddhhmmss
we can assume that you want [years][months][days][hours][minutes][seconds]
.
Based on that, we can break each part down as follows:
Years: If you want the full year then your yyyy
is correct and will prduce 2013
for example. A common (although not encouraged) alternative could be yy
(e.g. 13
)
Months: Currently your attempt of mm
does not return months. It will produce the minutes. You likely want MM
(e.g. 04
). Alternative include MMM
(e.g. APR
) and MMMM
(e.g. April
)
Days: Again you have this correct already. dd
will produce 18
for example.
Hours: Your attempt of hh
will produce a 12-hour time format. If this is what you are after then fine. But given that you have not attempted to include an AM/PM designator (with can be done with tt
by the way) then I would recommend you opt for the 24-hour format which is HH
(upper-case)
Minutes: You are correct with your minutes, mm
will produce 52
for example.
Seconds: Again, ss
is correct and will produce 33
for example.
Now we can string them all together and produce the following format, which includes the 24-hour time format which I would recommend. This can then be passed to the DateTime
objects ToString()
function like so:
var stringDateTime = slottingmessage.SlottingDateTime.ToString("yyyyMMddHHmmss");
If you wanted to maintain your approach of the GetDateTimeInNewFormat
method, then you can implement it like so:
public string GetDateTimeInNewFormat(DateTime dt)
{
return dt.ToString("yyyyMMddHHmmss");
}
This function would then be called the same way you already have in your example code:
GetDateTimeInNewFormat(slottingmessage.SlottingDateTime)
You can read more information about the various date/time formatting options here where there are plenty of examples
Upvotes: 0
Reputation: 25221
How about this?
public string GetDateTimeInNewFormat(DateTime d)
{
return d.ToString("yyyyMMddhhmmss");
}
Or, for 24h format:
public string GetDateTimeInNewFormat(DateTime d)
{
return d.ToString("yyyyMMddHHmmss");
}
Upvotes: 5