1,105,633 Community Members

Checking on Date and Time (C#)

Member Avatar
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
 
0
 

Is it Mother Date and Father Time? Any way, getting all the different date and time displays working can be imperturbably perplexing at times. You take a look at it in this Windows Console Application.

// retrieve and format date/time data
// tested with VCS.NET 2003

using System;  // has all the date/time stuff
  
class myApp
{
  public static void Main()
  {
    DateTime CurrTime = DateTime.Now;
  
    Console.WriteLine("DateTime display listing specifier and result:\n");

    Console.WriteLine("d = {0:d}", CurrTime );  // Short date mm/dd/yyyy
    Console.WriteLine("D = {0:D}", CurrTime );  // Long date day, month dd, yyyy
    Console.WriteLine("f = {0:f}", CurrTime );  // Full date/short time day, month dd, yyyy hh:mm
    Console.WriteLine("F = {0:F}", CurrTime );  // Full date/full time day, month dd, yyyy HH:mm:ss AM/PM
    Console.WriteLine("g = {0:g}", CurrTime );  // Short date/short time mm/dd/yyyy HH:mm
    Console.WriteLine("G = {0:G}", CurrTime );  // Short date/long time mm/dd/yyyy hh:mm:ss
    Console.WriteLine("M = {0:M}", CurrTime );  // Month dd
    Console.WriteLine("R = {0:R}", CurrTime );  // ddd Month yyyy hh:mm:ss GMT
    Console.WriteLine("s = {0:s}", CurrTime );  // yyyy-mm-dd hh:mm:ss  can be sorted!
    Console.WriteLine("t = {0:t}", CurrTime );  // Short time hh:mm AM/PM
    Console.WriteLine("T = {0:T}", CurrTime );  // Long time hh:mm:ss AM/PM
    Console.WriteLine("u = {0:u}", CurrTime );  // yyyy-mm-dd hh:mm:ss  universal/sortable
    Console.WriteLine("U = {0:U}", CurrTime );  // day, month dd, yyyy hh:mm:ss AM/PM
    Console.WriteLine("Y = {0:Y}", CurrTime );  // Month, yyyy
    Console.WriteLine();
    Console.WriteLine("DateTime.Month     = " + CurrTime.Month);      // number of month
    Console.WriteLine("DateTime.DayOfWeek = " + CurrTime.DayOfWeek);  // full name of day
    Console.WriteLine("DateTime.TimeOfDay = " + CurrTime.TimeOfDay);  // 24 hour time
    
    // number of 100-nanosecond intervals that have elapsed since 1/1/0001, 12:00am
    // useful for time-elapsed measurements
    Console.WriteLine("DateTime.Ticks     = " + CurrTime.Ticks);

    Console.Read();  // wait
  }
}
Member Avatar
dananos
Newbie Poster
6 posts since Mar 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It should be also mentioned that you can combine these as in DateTime.Now.ToString("dd/MM/yyyy h:MM tt") - for 13/12/2007 5:04 PM as an example

http://www.freebiesms.co.uk

Member Avatar
hwa
Newbie Poster
17 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

May I know how to make the following comparison:

A(Date/Time) + B(Integer, in HOUR) <= C.

if after A+B is next day of the date A then is should be increase one day then only compare with C.

How can I make it !!

Thanks in advance

Member Avatar
jakoo1983
Newbie Poster
1 post since Feb 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

If Iwant Do that in Windows Form
//.................................
DateTime CurrTime = DateTime.Now;
t1.text=CurrTime;

Member Avatar
sfarhan
Newbie Poster
1 post since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Farhan program for date validation

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

            string date =
Member Avatar
medelmhanga
Newbie Poster
1 post since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,
Suppose i want to write the code which will tell/great the user according to time e.g Goodmorning, goodvening e.t.c. How may i write this code.

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
1
 

You can convert DateTime value to an integer, to get the hour and minutes seperated values. And then use them to compare to the hours which defines to when you say good morning and afternoon.

DateTime time = DateTime.Now;
            int hour = time.Hour;
            int minutes=time.Minute;
            if (hour <= 12)
                Console.WriteLine("Good morning");
            else if (hour >= 12 && minutes >= 1)
                Console.WriteLine("Good afternoon");
            Console.ReadLine();

Hope it helps,
Mitja

Member Avatar
abiwax
Newbie Poster
2 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Pls how do I write d code for find and replace in c# for a notepad program

Member Avatar
Sokh
Newbie Poster
4 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi every one,PLS help me,I want to import Time and Date from a site to my program code in C# instead of using local Date and Time ,Can you help me?

Member Avatar
Tsawm
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you .. I got great help

You
Post:
Start New Discussion
View similar articles that have also been tagged: