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
Solved

Help with displaying date depending on IF statements

Posted on 2013-12-05
1
310 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
1 Comment
 
LVL 29

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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