8 Replies - 612 Views - Last Post: 01 December 2011 - 04:06 PM Rate Topic: -----

#1 thomas130  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-March 11

How do a if statement with time dates

Posted 01 December 2011 - 08:33 AM

Hi guys I'm trying to do an if statement to compared two dates if the first is less or equal then then do this exit if then

This is what I what I have done so far but I can see that I need do something more with the statement to be able to compared dates. Could you give me an example of how to do it.

           

FileInfo fileinfo = new FileInfo(@"\\ops.expressandstar.co.uk\KnowledgePlanningVersions\knowledge planning.zip");
            FileInfo fileinfo2 = new FileInfo(@"c:\Temp\knowledge planning.zip");
            DateTime time = fileinfo.CreationTime;
            DateTime time2 = fileinfo2.CreationTime;


         
            if (time = time2)
            {Environment.Exit(0);}
            else
            { Console.WriteLine("The program will continue"); }



This post has been edited by thomas130: 01 December 2011 - 08:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do a if statement with time dates

#2 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: How do a if statement with time dates

Posted 01 December 2011 - 08:40 AM

You want something similar to this:
if(time <= time2)
{
    Environment.Exit(0);
}
else
{
    // other code
}


You are assigning time1 to the value of time2, you need the == operator at the very least but in your case you wanted less than equals so you require <=

This post has been edited by batesy3k: 01 December 2011 - 08:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: How do a if statement with time dates

Posted 01 December 2011 - 08:46 AM

I am somewhat confused about what you are trying to do. Well, if you are doing something where if the date that current is '1' smaller than the date specified, you would do this:


TextReader tr = new StreamReader("date.txt");
            DateTime specdate = DateTime.Parse("12/1/2011 12:00:00 AM");
            if (DateTime.Today == specdate)
            {
                Console.WriteLine("Matches");
            }
            else
            {
                Console.WriteLine("Does not match!");
            }
            Console.ReadLine();




Edited code so it was more efficient.

This post has been edited by CreaturGames: 01 December 2011 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How do a if statement with time dates

Posted 01 December 2011 - 08:58 AM

DateTime can be compared using the greater than/less then/equal operators just like numbers.

if (time <= time2)
   Environment.Exit(0);
else
   Console.WriteLine("time 2 is less than time 1");



Also remember a single equals sign(=) is for assignment(string test = "test";).
Double equals signs(==) is for equality comparison( if (test == test2)).
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: How do a if statement with time dates

Posted 01 December 2011 - 09:13 AM

08 if (time = time2)


This is not a comparison of two variables. You are setting time equal to time2. A comparison requires the double equal sign.

if (time == time2)

I'm going to suggest you work more on learning C# from books, tutorials and courses before you try to engineer a purposeful application from scratch.


The stages of asking for homework help on a forum

Quote

I have a little programming experience but I need to write ...


You need to start there. I can't say "I have little experience in speaking Russian, but I have been assigned to write a mystery novel in Russian. Can you help me?"

We can help you by saying "First learn basic programming and the language of C#. Then take on assignments." Could someone here write this program for you? Sure. Could someone here map out all the processes you need to follow and do the Software Design part of this in the slim hope you could code it from there? Sure. But we don't volunteer to do the job that you're either getting paid for, or getting a grade for. You may want to read this.

For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Nov 2011
Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 thomas130  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-March 11

Re: How do a if statement with time dates

Posted 01 December 2011 - 10:30 AM

tlhIn`toq Icon I get what you saying and I have been reading book on the basics however I learn by doing, I did read around 200 pages on the introduction to c# until I realise the information is going in one ear and out the other.

I did the jump the gun a little before posting the thread as I managed to resolve the issue before I got a reply. Iím working on a project but Iím slowly getting there I done the basics stuff for my little program now I need to some more advanced stuff. But I will learn this stuff because of one reason is because I have too and that's how I learn.

Below is what I done so far. I know some people might laugh and think it very basic but I'm going to learn the more advance concepts to get to the stage I want.

However thank you for your advice.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ionic.Zip;
using System.Collections;

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

            //Knowledge Planning Script
            //Modified Date 18 October 2011

            // Display Banner
            Console.WriteLine("Knowledge Planning Update 1.3");


            // Determine whether the remote directory exists.
            Console.WriteLine("Confirming remote path exists");
            string remoteknowledgeplanning = @"\\ops.expressandstar.co.uk\KnowledgePlanningVersions\knowledge planning.zip";
            if (File.Exists(remoteknowledgeplanning))
            {
                Console.WriteLine("The path exists");
            }
            else
            {
                Console.WriteLine("The Knowledge Planning does not exist to be downloaded");
                Console.WriteLine("The application will now close");
                Environment.Exit(0);
            }



            //check whether or not the machine already has the up to date knowledge planning folder

            FileInfo fileinfo = new FileInfo(@"\\ops.expressandstar.co.uk\KnowledgePlanningVersions\knowledge planning.zip");
            FileInfo fileinfo2 = new FileInfo(@"c:\Temp\knowledge planning.zip");
            DateTime time = fileinfo.CreationTime;
            DateTime time2 = fileinfo2.CreationTime;
            
            if (time <= time2)
            {
                Console.WriteLine("Knowledge planning does'nt require updating");
                Environment.Exit(0);}
            else
            { Console.WriteLine("The program will continue"); }


            //remove old knowledge planning folder
            Console.WriteLine("Remove Old Knowledge Planning");
            string oldknowledgeplanning = @"C:\knowledge planning old";
            if (Directory.Exists(oldknowledgeplanning))
            {
                Console.WriteLine("The path exists");
                Directory.Delete(oldknowledgeplanning, true);
            }
            else
            {
                Console.WriteLine("Does'nt exists");

            }

            //rename Current knowledge planning folder
            Console.WriteLine("rename current knowledge planning");
            string currentknowledgeplanning = @"c:\knowledge planning";

            if (Directory.Exists(currentknowledgeplanning))
            {
                Console.WriteLine("The path exists");
                Directory.Move(currentknowledgeplanning, oldknowledgeplanning);

            }
            else
            {
                Console.WriteLine("Does'nt exists");
            }



            //Confirm how many files are within the zip directory
            int count = 0;
            ZipFile zip = ZipFile.Read(remoteknowledgeplanning);
            int C = zip.Count;
            for (int i = 0, D = zip.Count; i < D; i++)
            {
                ZipEntry e = zip[i];
                if (!e.IsDirectory) count++;
            }
            Console.WriteLine("There are {0} file entries in the zip archive.", count);



            //copy zip to local windows temp directory
            string copy = @"c:\Temp\knowledge planning.zip";
            if (File.Exists(copy))
            { File.Delete(copy);
            File.Copy(remoteknowledgeplanning, copy);
            }
            else
            {File.Copy(remoteknowledgeplanning, copy); }

            {
                string unzip = @"c:\";
                using (ZipFile zip1 = ZipFile.Read(copy))
                {
                    foreach (ZipEntry e in zip1)
                    {
                        e.Extract(unzip, ExtractExistingFileAction.OverwriteSilently);
                    }
                }
            }


        }

    }

}


Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: How do a if statement with time dates

Posted 01 December 2011 - 10:49 AM

I admire your persiverance. Glad to have you on board here.

I'm just going to suggest there is a wide middle ground between

Quote

I did read around 200 pages on the introduction to c# until I realise the information is going in one ear and out the other.


and trying to write your own application from scratch.

That middle ground is DOING TUTORIALS where the author has you DOING and not just reading. We learn coding by doing. Tutorials would include self-teaching books as they are basically a series of tutorials on paper in a pretty binding.

We have an entire series that takes a rookie from the beginning and moves them forward: Guided. With a plan.
http://www.dreaminco...t-c%23-program/

Please take a second look at that list of resources. If you *do* the tutorials and not just *read* them you will get a lot out of them and I think your learning will go a LOT faster.

Either way though - I think you have the right commitment to this. Happy to have you on board.
Was This Post Helpful? 1
  • +
  • -

#8 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: How do a if statement with time dates

Posted 01 December 2011 - 03:42 PM

@tlhIn`toq
I just had to 'thumbs up' this. The thing is, if you are new to programming, you need to aim at lower level applications, not jump into something more difficult than you can handle.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: How do a if statement with time dates

Posted 01 December 2011 - 04:06 PM

I agree completely. Which is why I gave him (in post 5) my recommendation to work more on tutorials and a complete resources list. Then backed up that recommendation in post 7.

You can lead a horse to water but...

And lets face it, we all have to stretch our wings and reach beyond our comfort zone in order to expand our skillset.

There is a difference between learning the basics and the language by writing a program from scratch (which results in horrible failure, crash and burn, morale degradation, automatic weapons purchase, clock tower sniper incidents, etc.) and learning through the books and tutorials - THEN - stretching by pushing into new areas from scratch.

All you can do is try to point this out and hope for the best choices to be made.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1