question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to Present in UTC, and Save in UTC. Cannot get data without timezone conversion

See original GitHub issue

Environment

Tech Version
material-ui-pickers 1.0.0-rc.4
material-ui 1.0.0-beta.33
React 16.2.0
Browser Chrome
Platform

Steps to reproduce

  1. Date & Time Picker Basic Example

Expected behavior

Component to send me back either: A) Format like so: 2018-04-04T13:48:00.000Z B) Accept my UTC=true flag, and NOT apply a timezone conversion as it is here: Fri Apr 20 2018 10:28:00 GMT-0700 (PDT)

Actual behavior

i) I can only receive the 2018-04-04T13:48:00.000Z format when I wrongly send a default value date string yyyy-mm-dd. This comes through as {}._i when the component submits. As soon as I send the proper selectedValue through, the {}._I property becomes Fri Apr 20 2018 10:28:00 GMT-0700 (PDT). ii) Am am applying UTC=true to the component. I can see _isUTC as true in the submission object. But the datetime format is still having the PDT conversion applied.

import React from 'react';
import moment from 'moment-timezone';
import MomentUtils from 'material-ui-pickers/utils/moment-utils';
import MuiPickersUtilsProvider from 'material-ui-pickers/utils/MuiPickersUtilsProvider';
import { DateTimePicker } from 'material-ui-pickers';

moment.tz.setDefault('UTC');

function DateAndTimePicker(props) {
  const { handleChange, label, selectedDate } = props;

  return (
    <MuiPickersUtilsProvider
      moment={ moment }
      utils={ MomentUtils } >
      <DateTimePicker
        disablePast
        label={ label }
        onChange={ handleChange }
        value={ selectedDate }
      />
    </MuiPickersUtilsProvider>
  );
}


export default DateAndTimePicker;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
tendermariocommented, Apr 15, 2018

I think he’s wanting the date to display in UTC instead of the local timezone, as the two expected behaviours listed are both in UTC. Is this possible?

4reactions
sibeliuscommented, Apr 1, 2020

this is my working solution

import MomentUtils from '@date-io/moment';

class MomentUTCUtils extends MomentUtils {
  format(value, formatString) {
    return this.moment.utc(value).format(formatString);
  }

  parse(value: string, format: string) {
    if (value === '') {
      return null;
    }

    return this.moment.utc(value, format, true);
  }

  date(value?: any) {
    if (value === null) {
      return null;
    }

    const moment = this.moment.utc(value);
    moment.locale(this.locale);

    return moment;
  }
}

export default MomentUTCUtils;
import React from 'react';

import { MuiPickersUtilsProvider } from '@material-ui/pickers';
import moment from 'moment';

import MomentUTCUtils from './MomentUTCUtils';

type Props = {
  children: React.ReactNode;
};
export const UTCWrapper = ({ children }: Props) => {
  return (
    <MuiPickersUtilsProvider utils={MomentUTCUtils} locale={'pt-br'} moment={moment}>
      {children}
    </MuiPickersUtilsProvider>
  );
};

usage:

<UTCWrapper>
   <DatePicker />
</UTCWrapper>

Using this could cause more harm then good

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to Present in UTC, and Save in UTC. Cannot get data ...
I can see _isUTC as true in the submission object. But the datetime format is still having the PDT conversion applied. import React...
Read more >
Parse date without timezone javascript - Stack Overflow
I want to parse time: without time zone. without calling a constructor Date.UTC or new Date(year, month, day) .
Read more >
Storing UTC is not a silver bullet | Jon Skeet's coding blog
The general advice of “just convert all local date/time data to UTC and store that” is overly broad in my view. For future...
Read more >
Converting old dates of UTC format to timezone and daylight
Converting old dates of UTC format to timezone and daylight saving time HiOur timezone is +02:00 from UTC (Greenwhich) time.
Read more >
Working with UTC time and converting it
Solved: Hi, I'm building an application for work travel, that is writing to a database that has the UTC timezone set, and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found