Get ISO string from date without time zone

My configuration is the time zone in Lisbon. When I do new Date(), I get the current local date / time, whichFri Apr 28 2017 01:10:55 GMT+0100 (GMT Daylight Time)

When I get the ISO string with toISOString(), it will apply the time zone, and I will get:

2017-04-28T00:10:55.964Z

The problem is that a few minutes ago the date was like this (yesterday):

2017-04-27T23:45:05.654Z

I tried moment.js (new to this) and I did something like this

document.write(moment('2017-04-28').format())

But I get this 2017-04-28T00:00:00+01:00one that is not2017-04-28T00:00:00.000Z

I want to pass this value as a parameter of the restful method for its automatic analysis as a DateTime type, but if I pass the output from the moment.js format, it will be parsed as 2017-04-27 23:00:00.00

new Date() new Date('2017-04-27') ( ), ISO, ,

2017-04-28T00:00:00.000Z

javascript, , toISOString() , , , ?

, , , .

+12
7

, . , UTC 0, UTC 0 toISOString, .

var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());

, UTC, , .

,

new Date('2017-04-27').toISOString();

2017-04-27T00: 00: 00Z ( UTC ECMA-262, ISO 8601, ), .

ISO 8601, :

if (!Date.prototype.toISODate) {
  Date.prototype.toISODate = function() {
    return this.getFullYear() + '-' +
           ('0'+ (this.getMonth()+1)).slice(-2) + '-' +
           ('0'+ this.getDate()).slice(-2);
  }
}

console.log(new Date().toISODate());

, toISOString UTC, . UTC, :

if (!Date.prototype.toUTCDate) {
  Date.prototype.toUTCDate = function() {
    return this.getUTCFullYear() + '-' +
           ('0'+ (this.getUTCMonth()+1)).slice(-2) + '-' +
           ('0'+ this.getUTCDate()).slice(-2);
  }
}

console.log(new Date().toUTCDate());
+9

, , , moment.format(), Z . Z , JS .

var date = moment('2014-08-28').format("YYYY-MM-DDT00:00:00.000") + "Z";

https://jsfiddle.net/2avxxz6q/

+5

, ISO 8601, API? , moment.js JavaScript, ?

:

document.write(moment('01/12/2016', 'DD/MM/YYYY', true).toISOString());

:

$(function(){
  var displaysTime = $('#divTime');
  displaysTime.text(moment.utc('27/04/2017', 'DD/MM/YYYY', true).toISOString());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div id="divTime">
</div>
0

moment.js, .

type Components = {
  day: number,
  month: number,
  year: number
}

export default class DateFormatter {
  // 2018-11-11T00:00:00
  static ISOStringWithoutTimeZone = (date: Date): string => {
    const components = DateFormatter.format(DateFormatter.components(date))
    return '${components.year}-${components.month}-${components.day}T00:00:00'
  }

  static format = (components: Components) => {
    return {
      day: '${components.day}'.padStart(2, '0'),
      month: '${components.month}'.padStart(2, '0'),
      year: components.year
    }
  }

  static components = (date: Date): Components => {
    return {
      day: date.getDate(),
      month: date.getMonth() + 1,
      year: date.getFullYear()
    }
  }
}
0

https://momentjs.com/docs/#/displaying/as-iso-string/

moment().toISOString(keepOffset);
0

startOf

moment().startOf("day").toISOString()
0

, , :

moment().tz("Europe/Lisbon").format('YYYY-MM-DD')

You can see that this works as you think:

moment('2017-04-28T00:00:00+01:00').tz("Europe/Lisbon").format('YYYY-MM-DD') //"2017-04-28"
-1
source

Source: https://habr.com/ru/post/1016845/


All Articles