JavaScript How to convert short dates to this format: DDD MMM DD YYYY GMT+0800 (X country standard time)?

When I do new Date() I get:

Thu Dec 28 2017 10:17:58 GMT+0800 (台北標準時間)

If I apply .valueOf() to that date I get:

1514427724039

Which is what I want.

Now, I need to apply .valueOf() to a date like this: 2017/12/28. I tried using Luxon to convert the date (since applying .valueOf() to YYYY/MM/DD doesn't produce a number):

DateTime.fromISO(startDate.replace(/\//g, '-')).toRFC2822()
// => Thu, 28 Dec 2017 00:00:00 +0800

However, applying valueOf() that results returns the same string. Not a number like in the first example.

What should I do so I can produce a numeric value from YYYY/MM/DD? Just I did with DDD MMM DD YYYY GMT+0800 (X country standard time)?

Answer:1

I think you're losing track of the types.

fromISO() returns Luxon DateTime object, but toRFC2822 returns an RFC 2822 string representation of the date.

So your valueOf() was being called on the string, not the DateTime.

As others have pointed out, you need only call valueOf() on the result of fromISO().

To illustrate:

var dt = luxon.DateTime.fromISO('2017-12-05'); // returns a Luxon DateTime object
console.log('fromISO returns an', typeof dt);

var rfc = dt.toRFC2822(); // returns a string
console.log('toRFC2822 returns a', typeof rfc);

var valueOfRFC = rfc.valueOf(); // string.valueOf() is just that string
console.log('strings are their own values?', rfc === valueOfRFC);

var valueOfDT = dt.valueOf(); // this is what you want
console.log('value of datetime', valueOfDT);
<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>
Answer:2

If you want to use Luxon you can:

var startDate = '2017/12/28';
var DateTime = luxon.DateTime;
// fromISO with regex
console.log( DateTime.fromISO(startDate.replace(/\//g, '-')).valueOf() );
// fromString instead of regex
console.log( DateTime.fromString(startDate, 'yyyy/MM/dd').valueOf() );
<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>
Answer:3

I am trying to change element class or any css property while dragging and it depends on elements position. So this is what i got so far: https://jsfiddle.net/twfegqgc/ So it changes when element ...

I am trying to change element class or any css property while dragging and it depends on elements position. So this is what i got so far: https://jsfiddle.net/twfegqgc/ So it changes when element ...

  1. change element class javascript
  2. change element class jquery
  3. change element class name javascript
  4. change element class on click
  5. change element class
  6. change element class angular
  7. change element class react
  8. change element class typescript
  9. change element class css
  10. change element class php
  11. change element class name
  12. change element class html
  13. change element class angularjs
  14. change element class in media query
  15. change element class using jquery
  16. javascript change element class
  17. change class element by id
  18. change class element vue
  19. change html element class javascript
  20. angular change element class on click

In my app.js file I have I am exporting an object with properties and values. On the route, I am including the file but I am getting the value as undefined. If I don't pass in the argument "users" in ...

In my app.js file I have I am exporting an object with properties and values. On the route, I am including the file but I am getting the value as undefined. If I don't pass in the argument "users" in ...

  1. undefined value from json
  2. value undefined in javascript
  3. value undefined in jquery
  4. value undefined in angular
  5. value undefined in php
  6. undefined value in math
  7. value of undefined
  8. undefined value in javascript object
  9. undefined value in python
  10. undefined value in array javascript
  11. undefined value in java
  12. undefined value in js
  13. undefined value in mongodb
  14. undefined value in excel
  15. undefined value in ajax
  16. undefined value in c#
  17. undefined value in perl
  18. undefined value in typescript
  19. undefined value in html
  20. ng model value undefined

I have an array that I want to transform into an object. For example: const arr = [{id: 1, key: ''}, {id: 2, key: ''}]; I want the result to be: const object = { 1: {id: 1, key: ''}, 2: { id: 2, ...

I have an array that I want to transform into an object. For example: const arr = [{id: 1, key: ''}, {id: 2, key: ''}]; I want the result to be: const object = { 1: {id: 1, key: ''}, 2: { id: 2, ...

I tried creating mixing in a getter into a JS object via the spread operator syntax, however it always seems to return null. HTML: <body> <div id="wrapperA"></div> <div id="...

I tried creating mixing in a getter into a JS object via the spread operator syntax, however it always seems to return null. HTML: <body> <div id="wrapperA"></div> <div id="...