简体   繁体   中英

Why is my DateTime.Now.Millisecond value always “000”?

I want my millisecond value to always be three characters long, so I'm padding it, where necessary, with "0"s:

private String getPlatypusFileName(String billNum)
{
    const string basePortion = "Platypus_";
    String PlatypusFileName;
    DateTime dt = DateTime.Now;
    int Year = dt.Year;
    int Month = dt.Month;
    int Day = dt.Day;
    int Hour = dt.Hour;
    int Minute = dt.Minute;
    int Second = dt.Second;
    int Millisecond = dt.Millisecond;
    String paddedBillNum = Prepad(6, billNum);
    String mon = Prepad(2, Month.ToString());
    String day = Prepad(2, Day.ToString());
    String hour = Prepad(2, Hour.ToString());
    String min = Prepad(2, Minute.ToString());
    String sec = Prepad(2, Second.ToString());
    String milli = Prepad(3, Millisecond.ToString());
    PlatypusFileName = String.Format("{0}{1}_{2}{3}{4}{5}{6}{7}_{8}.xml", 
        basePortion, paddedBillNum, Year, mon, day, hour, min, sec, milli);
    return PlatypusFileName;
}

private String Prepad(int finalSize, String originalVal)
{
    String paddedVal = originalVal;
    while (paddedVal.Length < finalSize)
    {
        paddedVal = "0" + paddedVal;
    }
    return paddedVal;
}

...but I'm always getting vals with three "0"s for the millisecond portion; the returned values are like so:

Platypus_000003_20141008145606_000.xml

Why would that be?

Your code should work fine, as written, except that you're probably running on Windows Compact Framework. As mentioned here , the CF always returns 0 for DateTime.Millisecond , so some other approach will be necessary if you need to have that accuracy, such as looking at Environment.TickCount .

That being said, you don't need to use Prepad here - just format appropriately:

const string basePortion = "Platypus_";
DateTime dt = DateTime.Now;
int billNum = 3;

string result = string.Format("{0}{1:000000}_{2}{3:00}{4:00}{5:00}{6:00}{7:00}_{8:00}.xml",
    basePortion, 
    billNum, 
    dt.Year,
    dt.Month,
    dt.Day,
    dt.Hour,
    dt.Minute,
    dt.Second,
    dt.Millisecond);

On my system, this prints out a value of: Platypus_000003_20141008121707_894.xml - Though that changes each time it's run, of course.

Why don't you use standard datetime.toString("special_format_string"). Learn methods of the string class, also learn about formats. You don't need even Prepad method.

All you need is one simple method:

    private String getPlatypusFileName(String billNum)
    {
        const string basePortion = "Platypus_";
        String PlatypusFileName;
        DateTime dt = DateTime.Now;
        PlatypusFileName = String.Format("{0}{1}_{2}.xml",
            basePortion, billNum.PadLeft(6,'0'), dt.ToString("yyyyMMddHHmmss_fff"));
        return PlatypusFileName;
    }

More over, don't use const if you don't know what it for. Also I'm sure that the base type of billNum source also is int , so you do not need any special method, you already have method string.Format() , just use it wisely. All your code can be replaced with one statement:

int billNum = 23;
string PlatypusFileName = string.Format("Platypus_{0:D6}_{1:yyyyMMddHHmmss_fff}.xml", billNum, DateTime.Now);

Since this is, indeed, a CF project and, as Reed Copsey pointed out, I'm doomed to zilches in that environment for milliseconds (and it seems seconds, too), I (since I don't really need accurate seconds/milliseconds values, just "something") did this:

    . . .
    String pseudoSeconds = GetRandomIntAsStr(10,59);
    String fauxMilliseconds = GetRandomIntAsStr(100, 999);
    return String.Format("{0}-{1}-{2} {3}:{4}:{5}.{6}", year, month, day, hour, minute, pseudoSeconds, fauxMilliseconds);
    . . .

// ad[a,o]pted from http://www.whypad.com/posts/csharp-get-a-random-number-between-x-and-y/412/
internal static String GetRandomIntAsStr(int min, int max)
{
    Random rnd = new Random();
    int val = rnd.Next(min, max);
    return val.ToString();
}

And I get values such as "44.766" for the randomized portions of the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM