Convert 20121004 (yyyyMMdd) to a valid date time?

前端 未结 4 1274
被撕碎了的回忆
被撕碎了的回忆 2020-12-25 09:32

I have a string in the following format yyyyMMdd and I am trying to get it to look like this:

yyyy-MM-dd

When I try:



        
4条回答
  •  难免孤独
    2020-12-25 09:59

    It's a little ugly, but how about this?

    date.Insert(6, "-").Insert(4, "-");
    

    If you can assume you're coming in with a string representing a valid date and you don't need to do any other date-ish logic, then why go to a DateTime in the first place?

提交回复
热议问题