Date and time in JavaScript with moment.js and moment-timezone.js
<p>I need to display <code>date and time</code> in input fields. In the first one <code>datetime value</code> of start of a day, and in second one <code>datetime value</code> of the end of a day. I use <code>moment.js</code> and <code>moment-timezone.js</code></p>

<ul>
<li><a href="http://momentjs.com/" rel="nofollow">moment.js</a></li>
<li><a href="http://momentjs.com/timezone/" rel="nofollow">moment-timezone.js</a></li>
</ul>

<h2>Problem</h2>

<p>From <code>00:00:00</code> to <code>02:59:59</code> every day code shows unexpected results.
Code shows previous day instead of current day.
Here is <a href="https://jsfiddle.net/90tov5h2/4/" rel="nofollow">jsFiddle example</a>.</p>

<p>In order to see what exactly I mean please open browser console and set your time to anything from <code>00:00:00</code> to <code>02:59:59</code> and region to <code>Europe/Riga</code>.</p>

<p>This problem shows on <code>Windows 10 Pro</code> and current Chrome, Firefox and Opera.</p>

<h2>Code</h2>

<pre><code>$(function() {
    var myTime = moment(new Date()).utc().toDate();
    myTime = moment(myTime).tz("Europe/Riga").format();
    console.log('myTime = '+ myTime);

    var startOfDay = moment().utc().startOf('day').subtract(3, 'h').toDate();
    var endOfDay = moment().utc().endOf('day').subtract(3, 'h').toDate();
    console.log('startOfDay = '+ startOfDay);
    console.log('endOfDay   = '+ endOfDay);
    startOfDay = moment(startOfDay).tz("Europe/Riga").format();
    endOfDay = moment(endOfDay).tz("Europe/Riga").format();
    console.log('startOfDay = '+ startOfDay);
    console.log('endOfDay   = '+ endOfDay);
    document.getElementById('date_from').value = startOfDay;
    document.getElementById('date_to').value = endOfDay;
});
</code></pre>

<h2>Console output</h2>

<pre><code>{--- console output start --------------------------------}
myTime = 2016-08-29T01:56:37+03:00
startOfDay = Sun Aug 28 2016 00:00:00 GMT+0300 (FLE Daylight Time)
endOfDay   = Sun Aug 28 2016 23:59:59 GMT+0300 (FLE Daylight Time)
startOfDay = 2016-08-28T00:00:00+03:00
endOfDay   = 2016-08-28T23:59:59+03:00
{--- console output end --------------------------------}
</code></pre>

<p>As one can see <code>myTime</code> is set to 29th of August, but <code>startOfDay</code> and <code>endOfDay</code> still returns 28th of August.</p>

<ul>
<li>Is there something I do wrong? </li>
<li>Am I missing something here?</li>
<li>Or is it a bug?</li>
</ul>

<h2>Update</h2>

<p>working code without <code>utc()</code> and <code>subtract(3, 'h')</code></p>

<pre><code>$(function() {
    var myTime = moment().toDate();
    myTime = moment(myTime).tz("Europe/Riga").format();
    console.log('myTime = '+ myTime);

    var startOfDay = moment().startOf('day').toDate();
    var endOfDay = moment().endOf('day').toDate();
    console.log('startOfDay = '+ startOfDay);
    console.log('endOfDay   = '+ endOfDay);
    startOfDay = moment(startOfDay).tz("Europe/Riga").format();
    endOfDay = moment(endOfDay).tz("Europe/Riga").format();
    console.log('startOfDay = '+ startOfDay);
    console.log('endOfDay   = '+ endOfDay);
    document.getElementById('date_from').value = startOfDay;
    document.getElementById('date_to').value = endOfDay;
});
</code></pre>

<p>Please advise.</p>

<p>Thank You for your time and knowledge.</p>