C#将字符串YYYYDDD转换为普通日期格式

时间:2015-08-15 20:49:51

标签: c# .net visual-studio datetime

快速提问。我试图遍历文件目录并将它们重命名为不是Julian的dateformat。所有文件都是DailyFile.YYYYddd。我想把它们改成DailyFile.mm-dd-yyyy。我似乎无法锻炼朱利安日期转换。有人可以提供一些帮助吗?谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Globalization;

namespace TestingFileRename
{
class Program
{
    static void Main(string[] args)
    {
        string[] dirs = Directory.GetFiles(@"C:\TestDirectory\", "MERSDLY.*");
        JulianCalendar juliCal = new JulianCalendar();
        foreach (string file in dirs)
        {   
            // Convert Julian date to MM-DD-yyyy format here and replace the filetail with it. 

            //file.move(file,filefront + new date string);
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

static void Main(string[] args)
{
    var dirs = Directory.GetFiles(@"C:\TestDirectory\", "MERSDLY.*");
    foreach (string file in dirs)
    {
        var parts = file.Split("."); 
        var year = new DateTime(int.Parse(parts[1].Substring(0,4)), 1, 1);
        year = year.AddDays(int.Parse(parts[1].Substring(4)) - 1);
        parts[1] = year.ToString("MM-dd-yyyy");
        File.Move(file,string.Join(".", parts));
    }
}