Skip to content

Commit

Permalink
fix: Accept number and string as date input in formatToTimeZone like …
Browse files Browse the repository at this point in the history
…in other functions
  • Loading branch information
prantlf committed Oct 6, 2018
1 parent e3cae4d commit abe59f6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/formatToTimeZone.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @module date-fns */

import parseDate from 'date-fns/parse'
import formatDate from 'date-fns/format'
import { findTimeZone, getUTCOffset } from 'timezone-support'

Expand Down Expand Up @@ -70,7 +71,7 @@ import { findTimeZone, getUTCOffset } from 'timezone-support'
*
* The result may vary by locale.
*
* @param {Date|String|Number} date - the original date
* @param {Date|String|Number} argument - the original date
* @param {String} formatString - the string of formatting tokens
* @param {Object} options - the object with options
* @param {Object} [options.locale=enLocale] - the locale object
Expand All @@ -97,13 +98,14 @@ import { findTimeZone, getUTCOffset } from 'timezone-support'
* )
* // Returns '12:00, 2-a de julio 2014 (+02:00 CEST)'
*/
function formatToTimeZone (date, formatString, options) {
function formatToTimeZone (argument, formatString, options) {
let date = parseDate(argument)
let { timeZone, convertTimeZone } = options
timeZone = findTimeZone(timeZone)
timeZone = getUTCOffset(date, timeZone)
if (convertTimeZone !== false) {
const offset = timeZone.offset - date.getTimezoneOffset()
date = new Date(date.valueOf() - offset * 60 * 1000)
date = new Date(date.getTime() - offset * 60 * 1000)
}
formatString = formatTimeZoneTokens(formatString, timeZone)
return formatDate(date, formatString, options)
Expand Down

0 comments on commit abe59f6

Please sign in to comment.