Solved

Help with displaying date depending on IF statements

Posted on 2013-12-05
1
312 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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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