David Frick
David Frick

Reputation: 776

How does one format YYYYMM as javascript date?

I am working with database data that is in YYYYMM format. I am hoping to present it in javascript for analysis. I have looked through the documentation for Intl.DateTimeFormat and was trying it out. However, it does not seem to interpret it correctly.

An example date is 201701 for Jan. of 2017.

console.log('201701')
console.log(new Intl.DateTimeFormat('en-US').format('201701'));  // Will give 12/31/1969 oddly enough

How do I format dates with YYYYMM format? I have not seen any answers to this question online or on SO and this seems to be a pretty common date format from databases. I am using vanilla Javascript and do not want to use moment.js as it is over kill for my needs.

Upvotes: 1

Views: 1188

Answers (4)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48723

You can write a trivial date parser.

Just find the start/end indices of a particular token. It it extensible too. You could easily add support for time.

const parseDate = (dateStr, format) => {
  const FMT_YEAR  = 'Y',
        FMT_MONTH = 'M',
        FMT_DATE  = 'D'
  
  const token = (str, startIndex, endIndex) => startIndex !== endIndex ?
    parseInt(str.substring(startIndex, endIndex + 1), 10) : 0
  
  let yearStartIndex  = format.indexOf(FMT_YEAR),
      yearEndIndex    = format.lastIndexOf(FMT_YEAR),
      monthStartIndex = format.indexOf(FMT_MONTH),
      monthEndIndex   = format.lastIndexOf(FMT_MONTH),
      dateStartIndex  = format.indexOf(FMT_DATE),
      dateEndIndex    = format.lastIndexOf(FMT_DATE)
      
  let year  = token(dateStr, yearStartIndex, yearEndIndex),
      month = token(dateStr, monthStartIndex, monthEndIndex) - 1,
      date  = token(dateStr, dateStartIndex, dateEndIndex) || 1
      
  return new Date(year, month, date)
}

console.log(parseDate('201701', 'YYYYMM').toString())

Here is a more concise version.

const parseDate = (dateStr, format) => {
  const extract = (dateStr, format, token) =>
    ((startIndex, endIndex) => startIndex !== endIndex ?
        parseInt(dateStr.substring(startIndex, endIndex + 1), 10) : 0
    )(format.indexOf(token), format.lastIndexOf(token))

  return new Date(
    extract(dateStr, format, 'Y'),
    extract(dateStr, format, 'M') - 1,
    extract(dateStr, format, 'D') || 1
  )
}

console.log(parseDate('201701', 'YYYYMM').toString())

Upvotes: 0

semirturgay
semirturgay

Reputation: 4201

You can try something like this:

const year = "201701".slice(0, 4)
const month = "201701".slice(4, 6)    
const dateObject = new Date(parseInt(year), parseInt(month) - 1)
console.log(dateObject)

Upvotes: 1

I think it could help:

let dateStr = '201701'
let year = dateStr.substring(0,4)
let month = parseInt( dateStr.substring(4,6) ) - 1

console.log(new Date(year, month))

Upvotes: 1

rksh1997
rksh1997

Reputation: 1203

dateStr = '201701'

date = new Date(dateStr.slice(0, 4) + '-' + dateStr.slice(4)) // Sun Jan 01 2017 02:00:00 GMT+0200 (Eastern European Standard Time)

Upvotes: 2

Related Questions