简体   繁体   English

C#:日期格式的代码优化

[英]C#: Code Optimization for Date Formatting

Using C#, I am trying to populate a variable with a known filename + date. 我正在使用C#尝试用已知的文件名+日期填充变量。 The catch is that the date must always be the last day of the previous month. 要注意的是,日期必须始终是上个月的最后一天。 It must also have no slashes or hyphens, and the year must be two digits. 还必须没有斜杠或连字符,并且年份必须是两位数。 For example, if it is now November 27 2015, I need my filename to be: Foobar_103115.txt 例如,如果现在是2015年11月27日,则我的文件名应为:Foobar_103115.txt

As a programmer who still has much to learn, I have written the clunky code below and it does achieve my desired result, even though it will obviously break after the end of this century. 作为一个仍有很多知识的程序员,我在下面编写了笨拙的代码,尽管它在本世纪末显然会中断,但它确实达到了我想要的结果。 My code is written this way because I could not figure out a more direct syntax for getting the date I want, complete with the specified formatting. 我的代码是用这种方式编写的,因为我无法找出一种更直接的语法来获取想要的日期,并带有指定的格式。

My question is this: What would be a more elegant and efficient way of recreating the below code? 我的问题是:重新创建以下代码会是哪种更优雅,更有效的方法?

I have commented all the code for any novice programmers who might be interested in this. 我已经对可能对此感兴趣的任何新手程序员注释了所有代码。 I know the experts I'm asking for help from don't need it. 我知道我正在寻求帮助的专家不需要它。

public void Main()
{
    String Filename
    DateTime date = DateTime.Today;

    var FirstDayOfThisMonth = DateTime.Today.AddDays(-(DateTime.Today.Day - 1)); //Gets the FIRST DAY of each month
    var LastDayOfLastMonth = FirstDayOfThisMonth.AddDays(-1); //Subtracts one day from the first day of each month to give you the last day of the previous month

    String outputDate = LastDayOfLastMonth.ToShortDateString(); //Reformats a long date string to a shorter one like 01/01/2015
    var NewDate = outputDate.Replace("20", ""); //Gives me a two-digit year which will work until the end of the century
    var NewDate2 = NewDate.Replace("/", ""); //Replaces the slashes with nothing so the format looks this way: 103115 (instead of 10/31/15)

    Filename = "Foobar_" + NewDate2 + ".txt"; //concatenates my newly formatted date to the filename and assigns to the variable

Sounds like you want something more like: 听起来您想要更多类似的东西:

// Warning: you should think about time zones...
DateTime today = DateTime.Today;
DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);
DateTime endOfPreviousMonth = startOfMonth.AddDays(-1);
string filename = string.Format(CultureInfo.InvariantCulture,
    "FooBar_{0:MMddyy}.txt", endOfPreviousMonth);

I definitely wouldn't use ToShortDateString here - you want a very specific format, so express it specifically. 我绝对不会在这里使用ToShortDateString您想要一种非常特定的格式,因此请专门表达它。 The results of ToShortDateString will vary based on the current thread's culture. ToShortDateString的结果将根据当前线程的区域性而有所不同。

Also note how my code only evaluates DateTime.Today once - this is a good habit to get into, as otherwise if the clock "ticks" into the next day between two evaluations of DateTime.Today , your original code could give some pretty odd results. 还要注意我的代码如何只对DateTime.Today一次评估-这是一个好习惯,否则,如果在两次DateTime.Today评估之间第二天时钟“滴答”,您的原始代码可能会给出一些非常奇怪的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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