I am working on a SVC service, and when I run it locally I get the Date field as follow 30/01/2020 00:00:00
and I parse this string to be a DateTime
as follow (DateTime.ParseExact(i["ProjectLastUpdate"].ToString(), "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString("yyyy'-'MM'-'dd'T'00':'00':'00'Z'"))
, since my local machine uses the United Kingdom regional setting. But when I host the service inside an Azure Web App, I start receiving the date string as follows: 1/29/2020 12:00:00 AM
, and the service will raise this exception “String was not recognized as a valid DateTime”
on the above code. So can anyone advice on this please? Can I standardize the date format on both local machine and azure? Also can I force my code to work on both environments?
Here is my full code, inside the service:-
using (ClientContext context = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = string.Format("<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{0}</Value></Eq></Where></Query></View>", listItemID);
ListItemCollection collListItem = context.Web.Lists.GetByTitle("Project Update System").GetItems(camlQuery);
context.Load(collListItem);
foreach (ListItem i in collListItem)
{
var mydate = (DateTime.ParseExact(i["ProjectLastUpdate"].ToString(), "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture).ToString("yyyy'-'MM'-'dd'T'00':'00':'00'Z'"));
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…