Author Topic: how to convert in date format for GetVariable("tdcreate")  (Read 1011 times)

0 Members and 1 Guest are viewing this topic.

amrit

  • Mosquito
  • Posts: 5
how to convert in date format for GetVariable("tdcreate")
« on: March 11, 2012, 06:23:09 pm »
hi
GetVariable("tdcreate")

when it return some this like dec value
so when it convert to cdate it give error.

is there any way to convert in Date and time format

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10566
  • class keyThumper<T>:ILazy<T>
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #1 on: March 11, 2012, 07:02:29 pm »
Because someone may ask for particulars ....

hi

i am useing vb.net 2010
Code - vb.net: [Select]
  1.        Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
  2.        Dim acaddoc As Autodesk.AutoCAD.Interop.AcadDocument
  3.        AcadApp = GetObject(, "Autocad.Application")
  4.        acaddoc = AcadApp.ActiveDocument
  5.        Dim DD As Double
  6.        DD = acaddoc.GetVariable("tdcreate")
how to convert DD value to date and time format

any suggestion will be great.

Thanks in advance.

Regards
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10566
  • class keyThumper<T>:ILazy<T>
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10566
  • class keyThumper<T>:ILazy<T>
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #3 on: March 12, 2012, 01:30:28 am »
playtime :
 :police: Not fully tested  :police:

Credit to Jon Flemming for a lisp routine ( JUL_CAL.LSP  ;;; Jon Fleming 9/22/91 )  which I converted .
;;; The formulae for the date calculations are modified from a sample
;;; application shipped with Doug Hamilton's C Shell, which in turn
;;; are based on routines published in Computer Language, December
;;; 1990.  The version here is modified to work properly with
;;; AutoCAD.

So this is almost full circle.
It could do with some optimisation, but I ran out of time today :)

Code - C#: [Select]
  1. // (C) CodeHimBelonga kdub
  2.  
  3. using System;
  4. using System.Globalization;
  5. using System.Windows;
  6. using Autodesk.AutoCAD.Runtime;
  7.  
  8. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  9. using Exception = System.Exception;
  10.  
  11. [assembly: CommandClass(typeof(kdubTesting.JulianConversion))]
  12.  
  13. namespace kdubTesting
  14. {
  15.    class JulianConversion
  16.    {
  17.        [CommandMethod("DoIt")]
  18.        public void JulianConversion_02()
  19.        {
  20.            var tdcreate = (double)AcadApp.GetSystemVariable("TDCREATE");
  21.            DateTime dt = ConvertAcadJulianToDateTime(tdcreate);
  22.  
  23.            AcadApp.ShowAlertDialog(dt.ToString("G", DateTimeFormatInfo.InvariantInfo));
  24.        }
  25.  
  26.  
  27.        private DateTime ConvertAcadJulianToDateTime(double julianDate)
  28.        {
  29.            DateTime date;
  30.            try
  31.            {
  32.                double z = Math.Floor(julianDate);
  33.                double w = Math.Floor((z - 1867216.25) / 36524.25);
  34.                double x = Math.Floor(w / 4);
  35.  
  36.                double a = (z + 1 + w - x);
  37.                double b = a + 1524;
  38.                double c = Math.Floor((b - 122.1) / 365.25);
  39.  
  40.                double d = Math.Floor(365.25 * c);
  41.                double e = Math.Floor((b - d) / 30.6001);
  42.                double f = Math.Floor(30.6001 * e);
  43.  
  44.                int day     = Convert.ToInt32(b - d - f);
  45.  
  46.                int m       = Convert.ToInt32((e < 14) ? (e - 2) : (e - 14));
  47.                int month   = (m + 1);
  48.  
  49.                int y       = Convert.ToInt32((m > 1) ? (c - 4716) : (c - 4715));
  50.                int year    = (y == 0) ? (y - 1) : y;
  51.                //
  52.                // strip the integer
  53.                double t        = (julianDate - z);
  54.                // hours since midnight
  55.                double hours    = Math.Floor(t * 24.0);
  56.                // temp value
  57.                double mins     = t - (hours / 24.0);
  58.                // 1440 minutes per day
  59.                double minutes  = Math.Floor(mins * 1440.0);
  60.                //
  61.                double seconds  = Math.Floor((mins - (minutes / 1440.0)) * 86400.0);
  62.  
  63.                date = new DateTime(year, month, day, Convert.ToInt32(hours), Convert.ToInt32(minutes), Convert.ToInt32(seconds));
  64.                return date;
  65.            }
  66.            catch (ArgumentOutOfRangeException ex)
  67.            {
  68.                MessageBox.Show("Julian date could not be converted:\n" + ex.Message,
  69.                                "Conversion Error", MessageBoxButton.OK, MessageBoxImage.Error);
  70.                date = new DateTime(0);
  71.            }
  72.            catch (Exception ex)
  73.            {
  74.                MessageBox.Show("Error converting Julian date:\n" + ex.Message, "Conversion Error",
  75.                                MessageBoxButton.OK, MessageBoxImage.Error);
  76.                date = new DateTime(0);
  77.            }
  78.            return date;
  79.        }
  80.    }
  81. }
  82.  
  83.  

« Last Edit: March 12, 2012, 01:35:46 am by Kerry »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

fixo

  • Swamp Rat
  • Posts: 856
  • My location is not in the Hitleropa
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #4 on: March 12, 2012, 01:44:40 am »
Not sure about maybe you could be able to parse
something like
Code: [Select]
Console.WriteLine(DateString, DateTime.FromBinary(dt))
~'J'~
« Last Edit: March 12, 2012, 06:25:57 am by fixo »
\\\"Always drink upstream from the herd."\\\ - Will Rogers, was died in 1935 plane crash

--> Donate to TheSwamp <--

amrit

  • Mosquito
  • Posts: 5
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #5 on: March 12, 2012, 01:50:17 am »
hi Kerry, thanks a lot,

it come perfect.


thank you very much
Amrit

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10566
  • class keyThumper<T>:ILazy<T>
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #6 on: March 12, 2012, 01:52:50 am »

Did the date-time formatting come in correctly for your system ??
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 10566
  • class keyThumper<T>:ILazy<T>
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #7 on: March 12, 2012, 06:46:41 am »
I'm now wondering is we could use DateTime.FromOADate Method to solve this.
http://msdn.microsoft.com/en-us/library/system.datetime.tooadate.aspx
Quote

An OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.
 
The base OLE Automation Date is midnight, 30 December 1899. The minimum OLE Automation date is midnight, 1 January 0100. The maximum OLE Automation Date is the same as DateTime.MaxValue, the last moment of 31 December 9999.
 
The ToOADate method throws an OverflowException if the current instance represents a date that is later than MinValue and earlier than midnight on January1, 0100. However, if the value of the current instance is MinValue, the method returns 0.
 
For more information about OLE Automation, see the MSDN Library.
< .. >

... something to investigate if the mood takes me :)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate<--

amrit

  • Mosquito
  • Posts: 5
Re: how to convert in date format for GetVariable("tdcreate")
« Reply #8 on: March 15, 2012, 11:46:20 am »
i have try DateTime.FromOADate  also. but i don't understand what is come. but from that code "(C) CodeHimBelonga kdub" it come same what i am looking for.