Skip to content

Commit

Permalink
Version 1.17.0: add toExcel() and toUtcExcel()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogier Schouten committed Mar 30, 2015
1 parent e6bc36d commit 0de665f
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 198 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
![license](http://img.shields.io/npm/l/timezonecomplete.svg)

[![NPM](https://nodei.co/npm/timezonecomplete.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/timezonecomplete/)
[![NPM](https://nodei.co/npm-dl/timezonecomplete.png?months=6&height=3)](https://nodei.co/npm/timezonecomplete/)
[![NPM](https://nodei.co/npm-dl/timezonecomplete.png?months=9&height=3)](https://nodei.co/npm/timezonecomplete/)


## Synopsis
Expand Down Expand Up @@ -359,7 +359,12 @@ var amsterdamDateNoDst = new tc.DateTime(2014, 1, 1, 13, 59, 59, 0, tc.zone("Eur
var amsterdamDateFromString = new tc.DateTime("2014-01-01T13:59:59.000 Europe/Amsterdam");

// date from an Excel datetime number
var excelDate = tc.DateTime.fromExcel(42005.5430555556); // 2015-01-01T13:02:00
var dt = tc.DateTime.fromExcel(42005.5430555556); // 2015-01-01T13:02:00

// Excel datetime number from a data
var dt = new tc.DateTime("2015-01-01T13:02:00 Europe/Amsterdam");
var excelAmsterdam = dt.toExcel(); // 42005.5430555556
var excelUtc = dt.toUtcExcel(); // 42005.501388888933333333333333333 (one hour earlier)

// a fully aware time without Daylight Saving Time: a fixed offset from UTC of 2 hours
var fixedOffset;
Expand Down Expand Up @@ -617,6 +622,8 @@ The version of the included IANA time zone database is 2015b.

## Changelog

### 1.17.0 (2015-03-30)
* Add DateTime#toExcel() and DateTime#toUtcExcel() functions to convert a DateTime to a Microsoft Excel date/time number.

### 1.16.1 (2015-03-26)
* Make DateTime constructor robust with respect to fractional numbers (it rounds to nearest millisecond)
Expand Down
28 changes: 28 additions & 0 deletions dist/timezonecomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,39 @@ var DateTime = (function () {
/**
* Create a DateTime from a Lotus 123 / Microsoft Excel date-time value
* i.e. a double representing days since 1-1-1900 where 1900 is incorrectly seen as leap year
* Does not work for dates < 1900
* @param n excel date/time number
* @param timeZone Time zone to assume that the excel value is in
* @returns a DateTime
*/
DateTime.fromExcel = function (n, timeZone) {
var unixTimestamp = Math.round((n - 25569) * 24 * 60 * 60 * 1000);
return new DateTime(unixTimestamp, timeZone);
};
/**
* Create an Excel timestamp for this datetime converted to the given zone.
* Does not work for dates < 1900
* @param timeZone Optional. Zone to convert to, default the zone the datetime is already in.
* @return an Excel date/time number i.e. days since 1-1-1900 where 1900 is incorrectly seen as leap year
*/
DateTime.prototype.toExcel = function (timeZone) {
var dt = this;
if (timeZone && !timeZone.equals(this.zone())) {
dt = this.toZone(timeZone);
}
var offsetMillis = dt.offset() * 60 * 1000;
var unixTimestamp = dt.unixUtcMillis();
return ((unixTimestamp + offsetMillis) / (24 * 60 * 60 * 1000)) + 25569;
};
/**
* Create an Excel timestamp for this datetime converted to UTC
* Does not work for dates < 1900
* @return an Excel date/time number i.e. days since 1-1-1900 where 1900 is incorrectly seen as leap year
*/
DateTime.prototype.toUtcExcel = function () {
var unixTimestamp = this.unixUtcMillis();
return ((unixTimestamp) / (24 * 60 * 60 * 1000)) + 25569;
};
/**
* @return a copy of this object
*/
Expand Down
2 changes: 1 addition & 1 deletion doc/assets/js/search.js

Large diffs are not rendered by default.

Loading

0 comments on commit 0de665f

Please sign in to comment.