943,264 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 276632
  • C# RSS
0

Checking on Date and Time (C#)

by on Feb 9th, 2005
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.
C# Code Snippet (Toggle Plain Text)
  1. // retrieve and format date/time data
  2. // tested with VCS.NET 2003
  3.  
  4. using System; // has all the date/time stuff
  5.  
  6. class myApp
  7. {
  8. public static void Main()
  9. {
  10. DateTime CurrTime = DateTime.Now;
  11.  
  12. Console.WriteLine("DateTime display listing specifier and result:\n");
  13.  
  14. Console.WriteLine("d = {0:d}", CurrTime ); // Short date mm/dd/yyyy
  15. Console.WriteLine("D = {0:D}", CurrTime ); // Long date day, month dd, yyyy
  16. Console.WriteLine("f = {0:f}", CurrTime ); // Full date/short time day, month dd, yyyy hh:mm
  17. Console.WriteLine("F = {0:F}", CurrTime ); // Full date/full time day, month dd, yyyy HH:mm:ss AM/PM
  18. Console.WriteLine("g = {0:g}", CurrTime ); // Short date/short time mm/dd/yyyy HH:mm
  19. Console.WriteLine("G = {0:G}", CurrTime ); // Short date/long time mm/dd/yyyy hh:mm:ss
  20. Console.WriteLine("M = {0:M}", CurrTime ); // Month dd
  21. Console.WriteLine("R = {0:R}", CurrTime ); // ddd Month yyyy hh:mm:ss GMT
  22. Console.WriteLine("s = {0:s}", CurrTime ); // yyyy-mm-dd hh:mm:ss can be sorted!
  23. Console.WriteLine("t = {0:t}", CurrTime ); // Short time hh:mm AM/PM
  24. Console.WriteLine("T = {0:T}", CurrTime ); // Long time hh:mm:ss AM/PM
  25. Console.WriteLine("u = {0:u}", CurrTime ); // yyyy-mm-dd hh:mm:ss universal/sortable
  26. Console.WriteLine("U = {0:U}", CurrTime ); // day, month dd, yyyy hh:mm:ss AM/PM
  27. Console.WriteLine("Y = {0:Y}", CurrTime ); // Month, yyyy
  28. Console.WriteLine();
  29. Console.WriteLine("DateTime.Month = " + CurrTime.Month); // number of month
  30. Console.WriteLine("DateTime.DayOfWeek = " + CurrTime.DayOfWeek); // full name of day
  31. Console.WriteLine("DateTime.TimeOfDay = " + CurrTime.TimeOfDay); // 24 hour time
  32.  
  33. // number of 100-nanosecond intervals that have elapsed since 1/1/0001, 12:00am
  34. // useful for time-elapsed measurements
  35. Console.WriteLine("DateTime.Ticks = " + CurrTime.Ticks);
  36.  
  37. Console.Read(); // wait
  38. }
  39. }
  40.  
Comments on this Code Snippet
Mar 19th, 2007
0

Re: Checking on Date and Time (C#)

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
Newbie Poster
dananos is offline Offline
6 posts
since Mar 2007
Mar 3rd, 2008
0

Re: Checking on Date and Time (C#)

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
hwa
Newbie Poster
hwa is offline Offline
17 posts
since Feb 2008
Feb 11th, 2009
0

Re: Checking on Date and Time (C#)

If Iwant Do that in Windows Form
//.................................
DateTime CurrTime = DateTime.Now;
t1.text=CurrTime;
Newbie Poster
jakoo1983 is offline Offline
1 posts
since Feb 2009
Aug 24th, 2010
-1

Farhan program for date validation check

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 =
Newbie Poster
sfarhan is offline Offline
1 posts
since Aug 2010
Nov 30th, 2010
0

Re: Checking on Date and Time (C#)

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.
Newbie Poster
medelmhanga is offline Offline
1 posts
since Nov 2010
Nov 30th, 2010
1

Re: Checking on Date and Time (C#)

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.
C# Syntax (Toggle Plain Text)
  1. DateTime time = DateTime.Now;
  2. int hour = time.Hour;
  3. int minutes=time.Minute;
  4. if (hour <= 12)
  5. Console.WriteLine("Good morning");
  6. else if (hour >= 12 && minutes >= 1)
  7. Console.WriteLine("Good afternoon");
  8. Console.ReadLine();

Hope it helps,
Mitja
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Aug 31st, 2011
0

Re: Checking on Date and Time (C#)

Pls how do I write d code for find and replace in c# for a notepad program
Newbie Poster
abiwax is offline Offline
2 posts
since Aug 2011
Oct 23rd, 2011
0

Re: Checking on Date and Time (C#)

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?
Newbie Poster
Sokh is offline Offline
4 posts
since Oct 2011
Nov 5th, 2011
0

Re: Checking on Date and Time (C#)

Thank you .. I got great help
Newbie Poster
Tsawm is offline Offline
1 posts
since Nov 2011
Message:
Previous Thread in C# Forum Timeline: MySQL Error in C# application
Next Thread in C# Forum Timeline: overloading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC