Bootstrap datetime picker picking day before selected - javascript

I am using this bootstrap datetime picker. I noticed that when I choose a day and convert the milliseconds using var d1 = new Date(milliseconds); it is converted into the day before my selected day. Is there a particular reason for this?
Example:
I select Tuesday, October 1st:
I log the date object after it is converted:

You must convert it into a Unix timestamp , which is a better way of tracking date/time.
Use new Date('your_date_string').getTime() / 1000 which gives you the timestamp or using PHP (strtotime) .
The date object that is being logged for you is probably coming from your system/browser settings(local).
Do not use JavaScript date and time calculations in web applications unless you ABSOLUTELY have to.
While you have the timestamp, cross-check if you are getting the correct time.

Related

javascript parses date string with browser timezone

var dts = "2019-05-26" // this value came from browser query like "d=1&date=2019-05-26"
var date = new Date(dts)
console.log(JSON.stringify(date))
which prints:
#=> "2019-05-25T19:00:00.0000Z"
Problem
I get this date from user input. Format only contains year, month and day. Problem happens when user browser's timezone applied on parsing. Sometimes, I get correct date in a day but sometimes i get one day before. This causes wrong database querying.
How can I convert this Date object to UTC? Because I need it as a Date object not as a string.
Is there any library that can help me parsing dates at UTC and get back as Date Object?
Use Moment UTC to normalize the time

Convert hour to date time without adding +1

Hi im using moment js to convert this string 20:00 I tried:
var a = moment("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00+01:00
the problem when I store in mongodb it become
2016-09-10T19:00:00.000Z
I want to store 2016-09-10T20:00:00.000Z
anyway can explain why please ?
When you say that you want to store 2016-09-10T20:00:00.000Z what you are saying is that you want to assume that your date and time is UTC.
To assume that the date you are parsing is a UTC value, use moment.utc
var a = moment.utc("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00Z
Note that when you parse a time without a date, moment assumes the current date. This may not be the behavior that you want.
I'm also not sure if you want a UTC date (which is what you are saying), or a local date without an offset indicator. If you want a local date without an offset indicator, simply use a format without an offset:
moment.utc("20:00", "HH:mm").format('YYYY-MM-DDTHH:mm:ss.SSS')
"2016-09-08T20:00:00.000"
If you are dealing with local dates that do not have a time zone association, I recommend using moment.utc to parse, as this will ensure that the time does not get shifted to account for DST in the current time zone.
For more information about how to parse dates into the time zone or offset that you would like in moment, see my blog post on the subject.
This it how it should look:
var a = moment("20:00", "HH:mm")
console.log(a.utcOffset('+0000').format())
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Doe, the problem is that you are using timezones when you create the date.
MomentJS uses your current timezone automatically.
Mongo however saves the time as it would be in another timezone.
Therefore, if you want the two strings to format the same way, you need to set the timezone.

Keeping the time fields but changing the timezone with moment.js

I have a moment object to which I need to apply a timezone, but without altering the values of the hour or minute field. For example, if my moment contains the date '2013-10-10T15:00:00+00:00' I want to be able to change the zone to 'America/Los_Angeles' so that when I print the moment I obtain '2013-10-10T15:00:00-07:00'
(for those of you familiar with Joda I'm after the withZoneRetainFields() functionality)
Here is a jsfiddle with the basic setup, showing the problem. How can I alter the last item so that it gives the desired output?
I recommend looking into the moment-timezone.js library for managing timezones. It offers an interesting .zone() function for manipulating timezones on a moment date object.
I've included a link to the relevent issue on moment.js's GitHub issues:
Switching timezone of moment without changing the values
Note, though I don't recommend it, you can also manually adjust a date for the desired timezone. In the below example, I'm creating a date in the client's timezone, changing the timezone on the date object to UTC, then fixing the value of the date such that the hours and minutes retain the same value.
// Takes a moment.js date object, and converts
// it to UTC timezone, while maintaining the
// selected hour/mins of the input date.
function convertDateToUTC (date){
// Current timezone's offset. Minutes offset from UTC
var offset = date.utcOffset();
// Convert selected date to UTC
date = date.utc();
// Adjust the time to.
date.add(offset, "minutes");
return date;
};

option to display the time in PST by converting milliseconds

i am working on javascript. and i have time in milliseconds. so i have to display the time in PST time Zone. i have tried for GMT. but is there any option to display the time in PST format in which the input is in milliseconds like 1671673550214. so iam trying to convert this milliseconds to time and date format of PST. same as the below code does. pleas help.
Here is my tried code:
var time = new Date().getTime();
var date = new Date(time);
document.write(date.toString());
i have even tried of using UTCString(), UTC() and toString(); but things is getting converted to GMT and not to PST.
Please check this library, it has lot of functions to display and manupilate date time.
http://momentjs.com/
http://momentjs.com/docs/
Also please check following post
Moment.js: Format date in a specific timezone\
If you dont want to use external lib, see the following article
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329
You can use timezonjs which help you to convert the time to any timezone.
https://github.com/mde/timezone-js

How to get difference of saved time and current time in jquery?

I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)

Categories

Resources