Solved

Help with displaying date depending on IF statements

Posted on 2013-12-05
1
313 Views
Last Modified: 2013-12-06
Hi there

I hope someone can help me, I have a date and I need to do the following:

if date:

1. Drop the year if it is the current year
2. Drop the date if it is 'today'
3. Drop the time if it is the previous  year

thanks
0
Comment
Question by:mousemat24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 39699404
You need different format strings. Please look at the code snippet. Code is for win forms but the idea is same for ASP. I have three buttons with different dates, see comments. Hope it will be clear.

        private string datreFormatStr1 = "{0:HH:mm}";
        private string datreFormatStr2 = "{0:dd MMMM HH:mm}";
        private string datreFormatStr3 = "{0:dd MMMM yyyy}";

        private void evaluteFormatAndDisplayDate(DateTime dt)
        {
            //1. Drop the year if it is the current year
            //2. Drop the date if it is 'today'
            //3. Drop the time if it is the previous  year
            string dateStr;
            int currentYear = DateTime.Now.Year;
            int currentMonth = DateTime.Now.Month;
            int currentDay = DateTime.Now.Day;

            if (dt.Year == currentYear)
            {
                if (dt.Month == currentMonth && dt.Day == currentDay)
                {
                    dateStr = string.Format(datreFormatStr1, dt);
                }
                else
                {
                    dateStr = string.Format(datreFormatStr2, dt);
                }

            }
            else
            {
                // Case3. Show year, month, day but not time
                dateStr = string.Format(datreFormatStr3, dt);
            }
            MessageBox.Show(dateStr);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //previous year
            DateTime dt = new DateTime(2012, 11, 27, 17, 55, 33);
            evaluteFormatAndDisplayDate(dt);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //this year but not today
            DateTime dt = new DateTime(DateTime.Now.Year, 10, 15, 13, 01, 27);
            evaluteFormatAndDisplayDate(dt);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //today
            DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 16, 22, 03);
            evaluteFormatAndDisplayDate(dt);
        }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question