Skip to content

Convert timestamp-with-timezone-objects from and to moment.js

License

Notifications You must be signed in to change notification settings

smartin85/moment-dt

Repository files navigation

moment-dt

MIT License npm version npm downloads Build Status Coverage Status Known Vulnerabilities

Buy me a coffee

Convert utc-unix-timestamp-with-timezone-objects from and to moment.js

Getting Started

moment-dt can be included in your app by different ways:

Node.js

moment-dt can be installed with npm and required into a script:

npm install --save moment-dt
var moment = require('moment-timezone');
require('moment-dt');

Browser

Just include the momentjs script and then the moment-dt script:

<script src="moment.js"></script>
<script src="moment-timezone-with-data.js"></script>
<script src="moment-dt.min.js"></script>

Browser with Require.js

define(["moment-timezone-with-data", "moment-dt"], function (moment) {
    // you probably won´t need a reference to moment-dt istself, so include it last
});

Bower

bower install --save moment-dt

Creating a moment from a dt-object

var obj = { ts: 1486920600, tz: "Europe/Berlin" };
var myMoment = moment.dt(obj);

myMoment.format();
// "2017-02-12T18:30:00+01:00"

moment.dt({ ts: null, tz: "Europe/Berlin" }).format(); 
// "Invalid date"

moment.dt({ ts: 1486920600, tz: null }).format(); 
// ERR: Moment Timezone has no data for invalid timezone.
// "2017-02-12T18:30:00+01:00" local time

Creating a dt-object from a moment

moment.dt({ ts: 1486920600, tz: "Europe/Berlin" }).dt();
// { 
//     ts: 1486920600, 
//     tz: "Europe/Berlin" 
// }

moment.dt({ ts: null, tz: "Europe/Berlin" }).dt();
// { 
//     ts: null, 
//     tz: "Europe/Berlin" 
// }

moment.dt({ ts: 1486920600, tz: null }).dt();
// ERR: Moment Timezone has no data for invalid timezone.
// { 
//     ts: 1486920600, 
//     tz: null 
// }

moment.dt({ ts: 1486920600, tz: "Europe/Berlin" }).add(1, 'year').dt();
// { 
//     ts: 1518456600, 
//     tz: "Europe/Berlin" 
// }