Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Putting in the date using in C#???

  • 01-11-2006 2:52am
    #1
    Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭


    Hey im in 1st year software development. We have to do write this program where about tax but we have to have it where the date shows up automatically without having to type it in every time. Can anyone tell me the line of code ya need for this?


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    If you can't find the answer in google within 15 seconds, let me know. Do you know how to get the date?


  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    I searched google and all i could find was this

    this.Label1.Text = DateTime.Now.ToString();

    but when i put that in nothing happens....I dont know how to get the date ( very new to this ):rolleyes:


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    This should work
    string date = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year ;
    


  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    nope still no date showing up.

    heres what the code looks like

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace work
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Declarations
    const double labourCharge = 25, partsVat = 0.10, labourVat = 0.00, discount = -0.05;
    string cusName;
    double labourHours, partsCost, gross, discountPrice, subTotal, labourCost, labourCostVat, partsCostVat, total;

    //Inputs

    string date = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;

    Console.Write("Please enter customer name: ");
    cusName = Console.ReadLine();

    Console.Write("Please enter hours of labour: ");
    labourHours = double.Parse(Console.ReadLine());

    Console.Write("Please enter cost of parts: ");
    partsCost = double.Parse(Console.ReadLine());

    //Processing
    labourCost = labourHours * labourCharge; //calculates cost of labour
    labourCostVat = labourCost * labourVat; //calculates cost of vat on labour
    partsCostVat = partsCost * partsVat; //calculates cost of vat on parts
    gross = labourCost + partsCost; //calculates the gross
    subTotal = labourCostVat + partsCostVat + gross; //calculates the subtotal
    discountPrice = subTotal * discount; //calculates the cost after the discount
    total = subTotal + discountPrice; //the final cost

    //Outputs
    Console.WriteLine("\nSparks Repair Shop");
    Console.WriteLine("\nCustomer Invoice");
    Console.WriteLine("\nName: {0}", cusName);
    Console.WriteLine("\nLabour:..............{0:c}", labourCost);
    Console.WriteLine("\nParts:...............{0:c}", partsCost);
    Console.WriteLine("\nGross:...............{0:c}", gross);
    Console.WriteLine("\nVat on parts:........{0:c}", partsCostVat);
    Console.WriteLine("\nVat on labour:.......{0:c}", labourCostVat);
    Console.WriteLine("\nSubTotal:............{0:c}", subTotal);
    Console.WriteLine("\nDiscount:...........{0:c}", discountPrice);
    Console.WriteLine("\nTotal:...............{0:c}", total);

    }
    }
    }


  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    You don't use the date variable anywhere in your code.

    The DateTime class returns the correct date for me.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    so what exactly am i doing wrong? (sorry im a total noob at this)


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    You're putting the current date into a string, called 'date', but then you're not doing anything with that, such as writing it to the console.

    Also, just a little tip, I think you can shorten the line:
    string date = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
    
    down to
    string date = DateTime.Now.toShortDateString();
    


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    stevenmu wrote:
    Also, just a little tip...
    Yes and no, depending on your formatting preference.

    The first line specifies a specific date format (yes there are better ways to do it, but anyhow), the second will be formatted according to the system locale. I realise that this is only a first year demo app, but should there be any future need for processing the date the differences could cause problems if mixed.


  • Registered Users, Registered Users 2 Posts: 6,535 ✭✭✭joe123


    Yes finally got it to work. Thanks to everyone who helped :)


Advertisement