- Home /
getting julian date in unity?
how can i use unity to convert the a date in the format dd/mm to it's day number [or julian dare]? so like july 3rd would be day 184 when it's not a leap year etc
Answer by Shah Mansur · May 11, 2014 at 08:48 AM
First of all Day Number number is not Julian Day
Day Number is very basic, DateTime class of C# provides you with Day Of Year which is total number of days since the start of current year and leap-year phenomenon is handled within the class, see the MSDN Documentation.
All you need to do is add System library to your code
using UnityEngine;
using System.Collections;
using System;
public class myClass: MonoBehaviour {
void Start () {
Debug.Log(DateTime.Now.DayOfYear.toString());
}
void Update(){
}
}
And as far as Julian Day is concerned, it is number of total days since the beginning of Julian Period.
Julian Period started at January 1, 4713 BC 12:00:00 hrs. So you need to calculate total number of days since Jan-1,4713 BC for that take the difference between the Year (Current or any) and 4713 BC. Count total number of leap years within that period.
Julian Day = ( Difference of Years * 365 ) + NumberOfLeapYearsInThisPeriod
What this will do is add 1 day for all leap years.
Julian Date is more precise than Julian Day and is in decimal representing how much of a day has passed, remember that Julian Period started at January 1, 4713 BC 12:00:00 hrs at noon, meaning Julian day starts & ends on mid day.
For today May-11,2014 Julian Day is 2456788. Which is true before noon 12:00 hrs after that the Julian Day is +1 which is 2456789. So you need to write algo to which considers 24 hrs as 1, 12 hours = 0.5. And so and so forth calculations down to milliseconds.
For May 11, 2014 11:00:00 hrs the Julian Date is 2456788.95833
Supposing we already have Julian Day, heres the code for Julian Date
int julianDay; //let's suppose we have value of Julian Day here
float julianDate;
now = DateTime.Now;
int mSecs = (((now.Hour * 60) + now.Minute) * 60 + now.Second) * 1000 + now.Millisecond;
julianDate = (mSecs < 43200000) ? --julianDay + (mSecs/43200000) : julianDay + (mSecs-43200000)/86400000;
Explanation is a day has 86400000 milliseconds and at mid day 43200000 millisecods. So by dividing time in milliseconds by 86400000 or 43200000 I get decimal portion of day or half day respectively.
Your answer
Follow this Question
Related Questions
Get System date 1 Answer
Count time when the game is not running (mobile device)? 2 Answers
Cleaning this code up. 2 Answers
How can I get the time since the epoch date in Unity3D? 2 Answers
putting the date into a variable 1 Answer