Get ISO string from a date without time zone

2020-06-08 00:46发布

My configuration is Lisbon time zone. When I do new Date() I get my current local date/time which is Fri 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 some minutes ago the date time 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:00 which is not 2017-04-28T00:00:00.000Z

I want to pass this value as a parameter of a restful method to parse it automatically as a DateTime type, but if I pass the output from moment.js format then it will get parsed as 2017-04-27 23:00:00.00

If I create a new date with new Date() or with new Date('2017-04-27') (date portion), I just want to get the ISO format like as follows with no time with no time zone

2017-04-28T00:00:00.000Z

Is there a javascript method like toISOString() or using maybe moment to get that format?

No matter what time zone or moment of day, I just to simulate that is midnight of the date given.

8条回答
做自己的国王
2楼-- · 2020-06-08 00:57

You can try this simple javascript

(new Date()).toJSON().replace(/\..*$/g,'')
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-06-08 01:00

From my understanding you want ISOString of start of the date irrespective of time

(ie) Time zone should be'T00:00:00.000Z' more matter what date you choose.

I dont think there is a straight function without using any plugin.

So i created a function to fullfill your need,If this is what you are looking for

function mimicISOString(date) {
    let d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    return [year, month, day].join('-') +'T00:00:00.000Z';
}

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

This will return current date with start of the time zone.If this is what you are looking for

查看更多
干净又极端
4楼-- · 2020-06-08 01:00

I believe you are saying you'd like to get the current date in Lisbon:

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

You can see that this works the way you think it should:

moment('2017-04-28T00:00:00+01:00').tz("Europe/Lisbon").format('YYYY-MM-DD') //"2017-04-28"
查看更多
Lonely孤独者°
5楼-- · 2020-06-08 01:03

I suppose you wish to get the current date in ISO 8601 to pass it to your API? Maybe using a moment.js wrapper for native JavaScript mentioned here might help?

As in your example:

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

Snippet:

$(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>

查看更多
SAY GOODBYE
6楼-- · 2020-06-08 01:04

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

moment().toISOString(keepOffset);
查看更多
淡お忘
7楼-- · 2020-06-08 01:09

You don't need moment.js library, you can just parse and connect date components. For example

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()
    }
  }
}
查看更多
登录 后发表回答