Saving Time with Meteor

Hi - I need some help. I have a form where a user is suppose to enter a date time. The problem is that it doesn’t seem to be saving the correct time. So the user is asked for a datetime and for some reason it’s saving a time 5 hours earlier than what the user enters and I can’t figure out why. The date and minutes are correct but not the hours. I’m also recording a submitted timestamp and that data is correct.

Also, I want to default this time to 8 hours earlier than the time right now - how can I do that? Basically when the form pulls up the default time shown will be 8 hours earlier than the time right now.

Any help is greatly appreciated. I’m not using autoforms. I’m building the forms on my own.

hey @sscaff1

does the user happen to be in a UTC+5 timezone? is there a timezone difference between the client and server?

if you parse the client input on the server, the input is interpreted relatively to the server’s timezone

if your timestamps are simply a new Date() invocation on the server, they’d sure to be correct as they represent the absolute present time on the server

Hey sscaff1,

Do you have any code to show? My first thought was that maybe your server was somehow out of sync, but you said that the submitted timestamp is correct so. Are you using moment.js?

I’m not - I actually did create a solution, but it’s probably not the right way to do it. See below.


  <div class="form-group">
    <label class="col-sm-3 control-label" for="startTime">Start Time:</label>
    <div class="col-sm-9 controls">
        <input name="startTime" id="startTime" type="datetime-local" class="form-control"/>
        Time has been defaulted. Please change your start time appropriately.


var s = $('[name=startTime]').val();
		var startTime = new Date(s.replace(/-/g,'/').replace('T',' '));

More JS (setting default time):

Template.shiftAdd.onRendered(function() {
	var current = new Date();
	var now = new Date(current - 60*13*60000);
	now = now.toISOString();
	now = now.substr(0, now.length - 8);
	document.getElementById("startTime").defaultValue = now;

My first thought was that I would use moment.js for stuff like that, would save you a ton of time, and give you more flexibility :slight_smile:

1 Like