0

I am making a sharepoint web appplication.

when I show the data from sharepoint lists, I want to change DateTime format type.

For example, "Created" item showed like this now [2017-06-15 PM 3:23:33]

But I only want to show [yyyy.mm.dd] format type.

How can I change that? my code is below.

 private void GetListGroupNews(StringBuilder sb, SPListItemCollection listItemCol) 
 { 
    try 
    { 
       if (listItemCol.Count > 0) 
       { 
          for (int i = 0; i < listItemCol.Count; i++) 
          { 
              string itemTitle = string.Empty; 
              string itemBody = string.Empty;  
              string itemRegDate = string.Empty; 

              itemTitle = listItemCol[i]["Title"] == null ? "" : listItemCol[i]["Title"].ToString(); 
              itemBody = listItemCol[i]["Body"] == null ? "" : listItemCol[i]["Body"].ToString(); 
              //I want to convert this
              itemRegDate = listItemCol[i]["Created"] == null ? "" : listItemCol[i]["Created"].ToString();

              //codes.....
          }
       }
    }
 }

1 Answer 1

1

You can use the below code:

DateTime date = Convert.ToDateTime(listItemCol[i]["Created"]);

itemRegDate = date.ToString("yyyy.mm.dd");
1
  • I modified your code a little bit, and it works well! thanks Gautam Sheth
    – Ryden Choi
    Jun 28, 2017 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.