from unix timestamp to datetime

前端 未结 8 1011
日久生厌
日久生厌 2020-12-01 09:28

I have something like /Date(1370001284000+0200)/ as timestamp. I guess it is a unix date, isn\'t it? How can I convert this to a date like this: 31.05.201

相关标签:
8条回答
  • 2020-12-01 09:44

    If using react:

    import Moment from 'react-moment';
    Moment.globalFormat = 'D MMM YYYY';
    

    then:

    <td><Moment unix>{1370001284}</Moment></td>
    
    0 讨论(0)
  • 2020-12-01 09:44

    I would like to add that Using the library momentjs in javascript you can have the whole data information in an object with:

    const today = moment(1557697070824.94).toObject();
    

    You should obtain an object with this properties:

    today: {
      date: 15,
      hours: 2,
      milliseconds: 207,
      minutes: 31,
      months: 4
      seconds: 22,
      years: 2019
    }
    

    It is very useful when you have to calculate dates.

    0 讨论(0)
  • 2020-12-01 09:51

    Looks like you might want the ISO format so that you can retain the timezone.

    var dateTime = new Date(1370001284000);
    dateTime.toISOString(); // Returns "2013-05-31T11:54:44.000Z"
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

    0 讨论(0)
  • 2020-12-01 09:52

    Without moment.js:

    var time_to_show = 1509968436; // unix timestamp in seconds
    
    var t = new Date(time_to_show * 1000);
    var formatted = ('0' + t.getHours()).slice(-2) + ':' + ('0' + t.getMinutes()).slice(-2);
    
    document.write(formatted);

    0 讨论(0)
  • 2020-12-01 09:55

    Import moment js:

    var fulldate = new Date(1370001284000);
    var converted_date = moment(fulldate).format(");
    
    0 讨论(0)
  • 2020-12-01 09:55

    if you're using React I found 'react-moment' library more easy to handle for Front-End related tasks, just import <Moment> component and add unix prop:

    import Moment from 'react-moment'
    
     // get date variable
     const {date} = this.props 
    
     <Moment unix>{date}</Moment>
    
    0 讨论(0)
提交回复
热议问题