For this tutorial, I will talk about how to convert a date string into a Unix Timestamp format. A Unix Timestamp is a time record of a particular time instance of how far that instance in milliseconds is away from the 0:0:0 of January 1st, 1970 in GMT+0 timezone. A positive timestamp is how far the time instance is after the 0:0:0 of January 1st time instance, 1970. A negative timestamp is how far the time instance is before the 0:0:0 of January 1st, 1970 time instance.

A Unix Timestamp consists of a whole number which represents seconds and three digits behind the decimal of which represents milliseconds. Most of us do not include millisecond into timestamp calculation. Thus the digits behind the decimal in a timestamp are usually removed. For the purpose of this demonstration, I will only use seconds and not milliseconds. For simplicity, we are going to only calculate the day as if we are at the 0:0:0 instance of that day.

For every type of programming language, there will most likely be a function that will convert a timestamp to date format or vice versa that built into the programming language. However, sometimes they are limited in function. In other events, we can encounter situations where we are working inside a very strict programming environment that do not give us the ability to import an external library. That event, in fact, did happen to me. This is why I wrote my own timestamp converter.

From that event, I learn one thing for certain. As programming language innovate to become better and better, sometimes we rely mostly on external library codes. Those additional libraries that we use in our program can help us efficient our programming process. However, sometimes it can make us become a little bit lazy to try to understanding the basic structure of how to solve our problems in principle. In my opinion, yes I do rely on the additional library when I program but when I have the time to learn, I will try to learn how that library solved my problem. Sometimes it sharpens my skill and knowledge.

The procedures for converting a timestamp without aid from another function can be a challenge. This is due to the fact that you have to keep track of leap year, century leap and quad century leap year. Keeping track of the leap counts is not the only challenge we have. We also have to know that they only occur after or before February the 29 depends on which direction we are to 1970.

This tutorial will only discuss in regard to converting date as in the zero hours of the day. For converting timestamp with hours, minutes and seconds. We first convert the date to a timestamp. If the time instance is after 1970, we can use this formula for calculating time: **seconds + (minutes* 60) + (hours * 60 * 60)**. After that, we would add the previous time value to our timestamp. If the time instance is before 1970, we would still calculate the timestamp for the date first. After that, we would use this formula for calculating the hours, minutes and seconds time of the date: **86400 – ( seconds + (minutes* 60) + (hours * 60 * 60) )**. Then we would take our timestamp value and minus off the previous equation result.

As for much, the code for this article was written in Ruby. Most of the explanation can be found within the script through commenting. The source code for this article is available in other programming languages.

In the future, I might incorporate the explanations into this article and remove the comments from the source code. Therefore this article and its source code may change.

For a Ruby’s built-in function that will convert a date to a timestamp. The code lines below can be utilized.

Date.new("2011-12-25").to_time.to_i

### Start Of Tutorial Script

—

**Advertisement**

# dateToTimeStamp function work on yyyy-mm-dd # This function feature is to convert a date string in the format of yyyy-mm-dd or yyyy/mm/dd to a Unix timestamp. # To read the entire tutorial please follow kevinhng86 blog @ http://kevinhng86.iblog.website . # There are other versions of this timestamp converter available on my website. # Without this function you can use Ruby's built-in function. # This function will work from the year 50 to 5500 due to how much a 64 bits long integer can hold. # Ruby built in library code: # Date.new("2011-12-25").to_time.to_i # class LibZ def self.dateToTimeStamp(_value) # This is the output value _out = 0 # An array for splitting input _datearray = Array.new # Without an input value, the script will end. if !(defined?_value) or _value.length < 1 raise "There is not an input value" # First, check for the format of yyyy-mm-dd elsif ( _value =~ /^\d{4}-{1}\d{1,2}-{1}\d{1,2}$/) # Split the digits into chunks and store into an array. _datearray = _value.split /-/ # Second, evaluate date in the format of yyyy/mm/dd elsif (_value =~ /^\d{4}\/{1}\d{1,2}\/{1}\d{1,2}$/) _datearray = _value.split("/") # If the input value does not match the previous date format, the program will exit with an error. else raise "Incorrect format" end # Convert the date values that stored inside the datearray into an integer value that corresponding to year, month and date. _inyyyy = _datearray[0].to_i _inmm = _datearray[1].to_i _indd = _datearray[2].to_i # Evaluate if the year is a leap year. _isLeap = ((_inyyyy % 4) === 0)? 1 : 0 # Evaluate if the year is a century leap year. _isCenturyLeap = ((_inyyyy % 100) === 0)? 1 : 0 # Evaluate if the year is a quad century leap year. _isQuadCenturyLeap = ((_inyyyy % 400 ) === 0)? 1 : 0 # Month value can't be greater than 12. if _inmm > 12 raise "Month cannot be greater than 12." # Date value can't be larger than 31. elsif _indd > 31 raise "Day cannot be great than 31." # Date value can't be larger than 29 if the month is February elsif (_inmm == 2 and _indd > 29) raise "February can't have more than 29 days" # For the month of April, June, September and November, the date value can't be greater than 30. elsif ( (_inmm == 4 and _indd > 30) or (_inmm == 6 and _indd > 30) or (_inmm == 9 and _indd > 30) or (_inmm == 11 and _indd > 30)) raise "April, June, September and November can't have more than 30 days" # Year, date or month's values can't be less than one. elsif (_inmm < 1 or _indd < 1 or _inyyyy < 1) raise "Month date and years input can't be less than 1" # The maximum support for this timestamp converter is a 64 bits long integer. Therefore, I safety net the below year to the year 50. elsif (_inyyyy < 50) raise "Memory doesn't allow us to convert year below 50" # The maximum support for this timestamp converter is a 64 bits long integer. Therefore I safety net the above year to the year 5500. elsif (_inyyyy > 5500) raise "Memory doesn't allow us to convert year above 5500" # If this is not a leap year then February can't have more than 28 days. elsif (_isLeap != 1 and _inmm == 2 and _indd > 28) raise "This is not a leap year, you can't have 29 days in Feburary" # If this is a century leap year but is not a quad centuary leap year then February can't have more than 28 days. elsif (_inmm == 2 and _isCenturyLeap == 1 and _isQuadCenturyLeap != 1 and _indd > 28) raise "This February can't have 29 days. Although it is a leap year, leap year is skipped on years that are divisible 100 years but will not skip if the year is divisible by 400" end # This array store the amount days that each month have. We count February as a constant of 28 days because we will modify our equation to match the leap year value. _dateinmonth = [31,28,31,30,31,30,31,31,30,31,30,31] # Variable for storing how many leap days occur since input year to 1972 or to 1968 _leapv = 0 # This is the index we would use to get the days from the dateinmonth array. _aridx2 = _inmm - 1 # Variable for storing the total date since input year to 1972 or 1968. _totaldate = 0 # Variable for storing the year from. _yeardate = 0 # Variable for storing how many days left in the current year of the input date. _dateleftinyear = 0 # Variable for storing how days left in the current month of the input value. _dateleftinmonth = 0 # What is a timestamp? A timestamp is a number string that contains a time value with the format of how many second since the 0:0:0 time of January 1, 1970. # Timestamps are actually measured in milliseconds, However, for this tutorial, we only use the second for measurement. # Negative timestamp is how far a time is below 1970. For instance, any date in 1969 and below will generate a negative timestamp. # Positive timestamp is how far a time is above 1970. For instance, any date in 1970 and above will generate a positive timestamp. 1970 1 1 in this tutorial will generate a 0 timestamp. # The first challenge we meet is the leap year. # Timestamp takes into account leap years, century leaps and quad century leaps. # What does leap years mean? # We get the 29th day in February if the year is divisible by four. We don't get the 29th day if the year is divisible by 100 but is not divisible by 400. # The method I prefer to use for calculating leap is if we are above 1970, we will use the closet below leap year. Which would be the year 1968 for calculating leap year value. # This is the fact that we only need to round down our number after we subtract the year value to 1968 then divide by four. # If we use 1970 every time we hit a decimal .5 or .75 we have to round up. # My formula for calculating time values that are above the 0:0:0 time of January 1st, 1970 is as follow: # Since we use leap to calculate to 1968, we will calculate to the 1st day of 1968 and then subtract off the result value a value of two 365 days years. # y = (input year value - 1968) * 365 # d = date from the beginning of year = 365 - ((total date in input month - input date) + (How many day left in the year since one month after the input month). # l = How many times leap year has occurred since 1968. Take input year - 1968 / 4. The whole number here is what we want not the decimal. If the value contained a decimal point, leap year had not happened yet for the decimal part of the value. # c = How many times century leap has occurred since 1968. Input year - 1968 / 100. The whole number here is what we want and not the decimal. # q = How many times quad century leap has occurred since 1968. Input year - 1968 / 400. The whole number here is what we want and not the decimal. # totaldate = y - d + l - c + q ; # timestamp = (totaldate * 86400) - 63158400 // 86400 is how many seconds that is in a day. and 63158400 is the amount of second that contains within two years that is 365 days each. # Since we are calculating to 1968 we have to subtract the amount of second in two years from the result value. # For the purpose of this tutorial I divide the code into what happen if the time value is above the 0:0:0 time of 1970 and what happen if it is below. # It is possible to combine the equation and modify the equation as desired. if _inyyyy >= 1970 _leapv = ((_inyyyy - 1968) / 4).to_i _centuryleapv = ((_inyyyy - 2000) / 100).to_i _quadcenturyleapv = ((_inyyyy - 2000) / 400).to_i _length = _dateinmonth.length _dateleftinmonth = _dateinmonth[_aridx2] - _indd # Get how many date left in a the year since after the input month. for _i in _inmm..._length do _dateleftinyear = _dateleftinyear + _dateinmonth[_i] end # Get how many days occurred since the begining of year to input date by subtracting 365 to (dateleftinmonth + dateleftinyear) _datefrombeginingofyear = 365 - (_dateleftinmonth + _dateleftinyear) # The real challenge we have for calculating leap is. # On the year that it is a leap year, we do not have the extra day until we have passed the 29th of February. # Hence, although in a leap year, if we have not passed the 29th day, we can't add leap values to our equation. # However, the automatic formula has already taken into account the values for leap year. # Therefore those values, have to be subtract off if we has not passed the 29 day of Feburary. # Each if loop below is associate with a type of leap calculation. if (_isLeap === 1 and _inmm < 3) _leapv = ((_inmm < 2) or (_inmm == 2 and _indd <= 29))? _leapv - 1 : _leapv end if (_isCenturyLeap === 1 and _inmm < 3) _centuryleapv = ( (_inmm < 2) or (_inmm === 2 and _indd <= 29) )? _centuryleapv - 1 : _centuryleapv end if (_isQuadCenturyLeap === 1 and _inmm < 3) _quadcenturyleapv = ( (_inmm < 2) or (_inmm === 2 and _indd <= 29) )? _quadcenturyleapv - 1 : _quadcenturyleapv end # After evaluate and obtains all necessary value for the formula, we would calculate the result. _yeardate = (_inyyyy - 1968) * 365 _totaldate = _yeardate + _datefrombeginingofyear + _leapv - _centuryleapv + _quadcenturyleapv _out = (_totaldate * 86400) - 63158400 else # My formula for calculating time values that are before the 0:0: time of January 1st, 1970 is as follow: # The method I prefer to use for calculating leap values is, if we are below the year of 1970, we will use the closet above leap year, which would be the year 1972 for leap values calculation. # Since we use the year 1972 for calculating leap values, we will calculate the second value to the 1st day of 1972, then we subtract two 365 days years from the equation. # y = (input year value - 1972 + 1) * 365 # We will have a negative value of how far we are away from 1972, but we have to add one year here. # # This is because 1971 can be considered to be one year away from 1972. # # However it is not. We are still within the time frame of 365 days away but not really one year away from 1972. # Because we are calculating how far we are below. # We simply just calculating how many days left in the year since the input date value. # The previous procedure will let us know how far we are away from the end of the year. # d = day left in the month = total day in input month - input date. # e = day left in the year = total day left in the year after the input month. # # We want all the number below to be positive now. # l = How many times leap year has occurred since 1972. 1972 - input year / 4. The whole number here is what we want and not the decimal value. The decimal values are leap year that has not occurred. # c = How many times century leap has occurred since 1972. 1972 - input year / 100. The whole number here is what we want and not the decimal value. # q = How many times century leap occur since 1972. 1972 - input year / 400. The whole number here is what we want and not the decimal value. # The number now are in positive so that mean the further negative the further it is away. # totaldate = y - d - e - l + c - q # timestamp = (totaldate * 86400) + 63158400 - 172800 # 86400 is how many seconds that is in a day. and 63158400 is the amount of second that contained within a time frame of two 365 days years. # # Since we calculate to 1972 we have to give back this value. # # We have to also subtract two days from the equation which would be 172800 seconds. # # In this formula, if we are on the 31st day of December 1969 to 1970 1 1. # # We will not have any day left in the month or any day left in the year. # # Hence that is a 0 value, but in true reality, we are one day away if we are counting from the zero hour. # # Thus we have to subtract one day from the equation. _leapv = ((1972 - _inyyyy) / 4).to_i _centuryleapv = ((2000 - _inyyyy) / 100) .to_i _quadcenturyleapv = ((2000 - _inyyyy) / 400).to_i _length = _dateinmonth.length # There is a difference when we are evaluating leap calculation for a time value that is before the time 0:0:0 of January 1st, 1970. # Leap values will not be added to the equation unless we have passed the first day of March. if (_isLeap == 1 and _inmm > 2) _leapv = ((_inmm > 2) or (_inmm == 3 and _indd >= 1))? _leapv - 1 : _leapv end if (_isCenturyLeap == 1 and _inmm > 1) _centuryleapv = ((_inmm > 2) or (_inmm == 3 and _indd >= 1))? _centuryleapv - 1 : _centuryleapv end if (_isQuadCenturyLeap == 1 and _inmm > 1) _quadcenturyleapv = ((_inmm > 2) or (_inmm == 3 and _indd >= 1))? _quadcenturyleapv - 1 : _quadcenturyleapv end _dateleftinmonth = _dateinmonth[_aridx2] - _indd for _i in _inmm..._length do _dateleftinyear = _dateleftinyear + _dateinmonth[_i] end _yeardate = (_inyyyy - 1972 + 1) * 365 _totaldate = _yeardate - _dateleftinmonth - _dateleftinyear - _leapv + _centuryleapv - _quadcenturyleapv _out = (_totaldate * 86400) + 63158400 - 172800 end return _out end end # Sample here, remove when using print "\n" print LibZ.dateToTimeStamp("0400-3-1") print "\n"

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.

Original Post Name: "How To Convert Date To Unix Timestamp Without Using A Built In Function – Ruby".

Original Post Link: http://kevinhng86.iblog.website/2017/01/21/how-to-convert-date-to-unix-timestamp-without-using-a-built-in-function-ruby/.

**Advertisement**