請教 C# 前輩
文字檔內容裡時間重複
2010/02/01,08:46,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:51,9001,9006,9000,9005
2010/02/01,08:52,9001,9006,9000,9005
2010/02/01,08:53,9001,9006,9000,9005
2010/02/01,08:54,9001,9006,9000,9005
2010/02/01,08:55,9001,9006,9000,9005
2010/02/01,08:56,9001,9006,9000,9005
2010/02/01,08:56,9001,9006,9000,9005
2010/02/01,08:56,9001,9006,9000,9005
2010/02/01,08:59,9001,9006,9000,9005
2010/02/01,08:59,9001,9006,9000,9005
2010/02/01,09:01,9001,9006,9000,9005
2010/02/01,09:02,9001,9006,9000,9005
要轉換成以下內容後存新檔
日期 2010/02/01 --> 2010/2/1
時間重複部分自動進位 ex 08:47 , 08:56 ,08:59
2010/2/1,08:46,9001,9006,9000,9005
2010/2/1,08:47,9001,9006,9000,9005
2010/2/1,08:48,9001,9006,9000,9005
2010/2/1,08:49,9001,9006,9000,9005
2010/2/1,08:50,9001,9006,9000,9005
2010/2/1,08:51,9001,9006,9000,9005
2010/2/1,08:52,9001,9006,9000,9005
2010/2/1,08:53,9001,9006,9000,9005
2010/2/1,08:54,9001,9006,9000,9005
2010/2/1,08:55,9001,9006,9000,9005
2010/2/1,08:56,9001,9006,9000,9005
2010/2/1,08:57,9001,9006,9000,9005
2010/2/1,08:58,9001,9006,9000,9005
2010/2/1,08:59,9001,9006,9000,9005
2010/2/1,09:00,9001,9006,9000,9005
2010/2/1,09:01,9001,9006,9000,9005
2010/2/1,09:02,9001,9006,9000,9005
日期 2010/02/01 --> 2010/2/1
用時間ToString 後面可以帶要呈現的日期格式
程式碼如下:
string date = DateTime.Now.ToString("yyyy/M/d hh:mm");
如果要我們幫你解決
時間重複部分自動進位 ex 08:47 , 08:56 ,08:59
還要請你提供你的程式碼
using System;
public class Program
{
public static void Main()
{
string sInput = @"
2010/02/01,08:46,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:51,9001,9006,9000,9005
2010/02/01,08:52,9001,9006,9000,9005
2010/02/01,08:53,9001,9006,9000,9005
2010/02/01,08:54,9001,9006,9000,9005
2010/02/01,08:55,9001,9006,9000,9005
2010/02/01,08:56,9001,9006,9000,9005
2010/02/01,08:56,9001,9006,9000,9005
2010/02/01,08:56,9001,9006,9000,9005
2010/02/01,08:59,9001,9006,9000,9005
2010/02/01,08:59,9001,9006,9000,9005
2010/02/01,09:01,9001,9006,9000,9005
2010/02/01,09:02,9001,9006,9000,9005";
using (System.IO.StringReader reader = new System.IO.StringReader(sInput))
{
bool bFirst = true;
string sKey = @"";
string sLine;
while ((sLine = reader.ReadLine()) != null)
{
if (!sLine.Equals(sKey))
{
if (bFirst==true) {
bFirst = false;
} else {
Console.WriteLine(sKey.Replace("/0","/"));
}
sKey = sLine;
}
}
Console.WriteLine(sKey.Replace("/0","/"));
}
}
}
我沒看清楚題目
只有「刪除重覆資料」的部份
沒有「補資料」的部份...
謝謝您的回覆,如果要讓上面時間重複的部分,在迴圈判斷中會自動進位可以用甚麼指令 ?
沒有你要的「自動進位」的指令
要整理出資料的規律性
然後自己寫
舉例來說,以下這四筆資料
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:47,9001,9006,9000,9005
要改成
2010/02/01,08:47,9001,9006,9000,9005
2010/02/01,08:48,9001,9006,9000,9005
2010/02/01,08:49,9001,9006,9000,9005
2010/02/01,08:50,9001,9006,9000,9005
問題來了
2010/02/01,08:47,9001,9006,9004,9005
2010/02/01,08:47,9002,9003,9004,9005
2010/02/01,08:47,9003,9005,9001,9006
2010/02/01,08:47,9001,9005,9002,9007
如果會的話
那麼要如何決定
那一筆改 08:48
那一筆 08:49
那一筆 08:50 呢?
您好,自動進位的意思是,與上一列分鐘相同就加一分鐘,所以要時間相加的函數,進位一定是一分鐘進位
謝謝回覆與包容初接觸的新手
你所謂的「進位」如下,很簡單
using System;
public class Program
{
public static void Main()
{
string dateTime = "2010/2/1 08:47";
DateTime dt = Convert.ToDateTime(dateTime);
Console.WriteLine("{0} {1} {2} {3} {4}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute);
DateTime dtOneMin = dt.AddMinutes(1);
Console.WriteLine("{0} {1} {2} {3} {4}", dtOneMin.Year, dtOneMin.Month, dtOneMin.Day, dtOneMin.Hour, dtOneMin.Minute);
}
}
困難的在於判斷「什麼情形」要進位
如果現在考慮的規則越簡單(eg.重覆就進位)
未來碰到例外情形的可能就越大
Good luck.
如果你想要的只是時間相加的函數,DateTime的AddMinutes(1)就可以了吧。
謝謝各位大大的回覆,我先自習一下,不懂再上來請教!