zoukankan      html  css  js  c++  java
  • C#实现根据传入时间段,找出时间段内日期,并生成相对应文件路径

    【1】获取固定日期范围内的所有日期,以数组形式返回

    /// <summary>
            /// 获取固定日期范围内的所有日期,以数组形式返回
            /// </summary>
            /// <param name="startTime"></param>
            /// <param name="endTime"></param>
            private DateTime[] GetAllDays(DateTime startTime ,DateTime endTime) {
                List<DateTime> listDay = new List<DateTime>();
                DateTime dtDay = new DateTime();
                //循环比较,取出日期;
                for (dtDay = startTime; dtDay.CompareTo(endTime) <= 0;dtDay = dtDay.AddDays(1))
                {
                    listDay.Add(dtDay);
                }
                return listDay.ToArray();
            }

    我们来检测一下,在Main函数中编写如下代码:

    static void Main(string[] args)
            {
                System.DateTime currentTime = System.DateTime.Now;
                DateTime startTime = currentTime.AddDays(-11);
                DateTime endTime = currentTime.AddDays(-1);
                //Console.WriteLine(currentTime);
                Program getDate = new Program();
                DateTime[] dateList = getDate.GetAllDays(startTime, endTime);
                for (int i = 0; i < dateList.Length; i++)
                {
                    Console.WriteLine(dateList[i]);
                }
            }

    运行结果如下:

    2017/6/18 14:58:18
    2017/6/19 14:58:18
    2017/6/20 14:58:18
    2017/6/21 14:58:18
    2017/6/22 14:58:18
    2017/6/23 14:58:18
    2017/6/24 14:58:18
    2017/6/25 14:58:18
    2017/6/26 14:58:18
    2017/6/27 14:58:18
    2017/6/28 14:58:18

    【3】我们顺便再拓展一点,根据上面的日期再生成路径

      /// <summary>
            /// 根据传入时间段生成路径
            /// </summary>
            /// <param name="filePath"></param>
            public List<String> GeneratePath(DateTime startTime,DateTime endTime) {
                string FileName;
                List<String> filePathList = new List<string>();
                string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveDir";
                String filePath = "";
                DateTime[] SubTimeList = GetAllDays(startTime,endTime);
                for (int i = 0; i < SubTimeList.Length;i++ )
                {
                    string subStr = SubTimeList[i].ToString("yyyyMMdd");
                    FileName = "MyFileSend" + subStr + ".txt";
                    filePath = CurDir + FileName;
                    filePathList.Add(filePath);
                }
                return filePathList;       
            }

    我们来检测一下,在Main函数中编写如下代码:

    /// <summary>
            /// 获取文件中的数据
            /// </summary>
            /// <param name="filePath"></param>
            public static string fileToString(String filePath){
                string strData = "";
                try
                {
                    string line;
                    // 创建一个 StreamReader 的实例来读取文件 ,using 语句也能关闭 StreamReader
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
                    {                  
                        // 从文件读取并显示行,直到文件的末尾
                        while ((line = sr.ReadLine()) != null)
                        {
                            //Console.WriteLine(line);
                            strData = line;
                        }                    
                    }              
                }
                catch (Exception e)
                {
                    // 向用户显示出错消息
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }
                return strData;
            }

            static void Main(string[] args)
            {
                System.DateTime currentTime = System.DateTime.Now;
                DateTime startTime = currentTime.AddDays(-11);
                DateTime endTime = currentTime.AddDays(-1);
                //Console.WriteLine(currentTime);
                Program getDate = new Program();
                DateTime[] dateList = getDate.GetAllDays(startTime, endTime);
                List<String> filePathList = new List<string>();
                filePathList = getDate.GeneratePath(startTime, endTime);
                Console.WriteLine(filePathList);
                for (int i = 0; i < filePathList.Count; i++ )
                {
                    Console.WriteLine(filePathList[i]);
                }
            }

    运行结果如下:

    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170618.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170619.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170620.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170621.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170622.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170623.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170624.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170625.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170626.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170627.txt
    D:VSProjectSavaProcessToFileSavaProcessToFileinDebugSaveDirMyFileSend20170628.txt

    好了,不早了,今天就讲到这儿了;

    本文源于zhuxiaoge(http://www.cnblogs.com/zhuxiaoge/p/7094513.html),如有转载请标明出处,不甚感激!!!

  • 相关阅读:
    verilog编码规范
    verilog代码 想法验证---与寄存器输出有关
    MMCM与PLL
    Vivado约束文件(XDC)的探究(2)
    Vivado约束文件(XDC)的探究(1)
    VGA图像显示组成模块分析
    关于Quad PLL /CPLL参考时钟的选择
    GTX的生成(包括COMMON)
    SD-SDI播出系统---使用GTX TX产生恢复时钟
    DRP端口描述
  • 原文地址:https://www.cnblogs.com/zhuxiaoge/p/7094513.html
Copyright © 2011-2022 走看看