简体   繁体   中英

How to calculate start of day and end of day from a date in c#

I have a date coming into my c# program like this: "01/15/2015", and I need to translate that to a string like this: "2015-01-15T00:00:00Z" (ie, the beginning of that day). I am calling a 3rd party api that expects that format.

Then I need to take the same date and convert it to this: "2015-01-15T23:59:59Z" (ie, the end of the day given).

Here is what I have, which appears to work based on the limited testing I've done, but I am wondering if this is prone to errors or if there is a better way to accomplish this. I haven't worked with dates in this format before so I thought I'd ask those with more experience. Will T23:59:59Z be the end of the day in the time zone my server is on?

program example:

    class Program
    {
        static void Main(string[] args)
        {
            Search("01/15/2015");
        }

         private static void Search(string date) 
        {
            //produce this:                                    
            //string startOfDay = "2015-01-15T00:00:00Z";                        
            //string endOfDay = "2015-01-15T23:59:59Z";

            DateTime temp1 = DateTime.Parse(date);
            string startOfDay = temp1.ToString("yyyy-MM-ddTHH:mm:ssK") + "Z";

            DateTime temp2 = DateTime.Parse(date);
            string endOfDay = temp2.ToString("yyyy-MM-ddT") + "23:59:59Z";
        }
    }

Start of day is easy; you can just use .Date .

FYI: Please make sure you check culture.

在此处输入图片说明

class Program
{
    static void Main(string[] args)
    {

        DateTime date;
        if (DateTime.TryParse("01/15/2015", out date))
        {
            var startOfDay = date.Date;
            Console.WriteLine(startOfDay.ToString("s") + "Z");

            var endOfDay = date.ToEndOfDay();
            Console.WriteLine(endOfDay.ToString("s") + "Z");
        }

        Console.ReadLine();
    }
}

public static class DateExtensions
{
    public static DateTime ToEndOfDay(this DateTime date)
    {
        return date.Date.AddDays(1).AddTicks(-1);
    }
}
private static void Search(string date)
{
    DateTime parsedDate;
    if (DateTime.TryParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
    {
        var dateString = parsedDate.ToString("yyyy-MM-dd");
        var dateStart = dateString + "T00:00:00Z";
        var dateEnd = dateString + "T23:59:59Z";
    }
}

This completely ignores time zones or UTC, it simply converts the incoming string to a DateTime representation and then creates the 2 strings which is that date instance formatted and appended the hard coded beginning of day and end of day as a string.

If you want to do it completely in ANSI C here ya go - And this code snip will let you goof around with it at the char level of building the time/date string from scratch. Cheers...

char time_str[22], swap_str[3]; // my time string to build and swap data

void append_tdata(int t_unit, char delimiter); // generic append with dl

main(void)
{
   time_t t;          // I believe everything here is ANSI C
   struct tm *gmt;    // and should be very portable
   t = time(NULL);

   gmt = gmtime(&t);  // get zulu time

   // start building my string with the year
   itoa(gmt->tm_year + 1900, time_str, 10);

   append_tdata(gmt->tm_mon, '-');   // YYYY-MM
   append_tdata(gmt->tm_mday, '-');  // YYYY-MM-DD
   append_tdata(gmt->tm_hour, 'T');  // YYYY-MM-DDTHH
   append_tdata(gmt->tm_min, ':');   // YYYY-MM-DDTHH:MM
   append_tdata(gmt->tm_sec, ':');   // YYYY-MM-DDTHH:MM:SS
   time_str[strlen(time_str) + 1] = 0x0;
   time_str[strlen(time_str)] = 'Z'; // YYYY-MM-DDTHH:MM:SSZ

   // time_str build is done - do with it as you like
}

//---------------------------------------------------------------
void append_tdata(int t_unit, char delimiter)
{
   time_str[strlen(time_str) + 1] = 0x0;
   time_str[strlen(time_str)] = delimiter;

   if(t_unit < 10) // is the number added to string only one digit?
     {             // if so - pad it with a zero
       swap_str[0] = '0';
       itoa(t_unit, &swap_str[1], 10);
     }
   else
    itoa(t_unit, swap_str, 10); // otherwise just give me the number

   strcat(&time_str[strlen(time_str)], swap_str); // add it to my string plz
}

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