option to display the time in PST by converting milliseconds - javascript

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

Related

Calculate duration between momentjs timestamps in UTC

I am having a hard time to calculate the duration (difference in time) of two timestamps.
There is a timestamp I receive from the server in the following format:
start # => "2017-05-31 06:30:10 UTC"
(This is basically rubys DateTime.now.utc)
I want to see how many hours have passed since then until as of right now.
The calculation happens in the angularjs frontend only.
I tried the following:
var start = moment("2017-05-31 06:30:10 UTC", "YYYY-MM-DD HH:mm:ss Z").utc();
var now = moment.utc();
var duration = moment.duration(now.diff(start));
console.log(duration.asHours()); #=> 2 hours even though the point in time was just a couple of minutes ago.
Unfortunately this would always use my devices local time and produce a time that's a few hours off the actual time.
So my approach was to either convert all times to UTC and let momentjs handle all this.
What am I missing?
Since your input is UTC you can use moment.utc method.
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
Here a live example:
var start = moment.utc("2017-05-31 06:30:10 UTC", "YYYY-MM-DD HH:mm:ss Z");
var now = moment.utc();
var duration = moment.duration(now.diff(start));
console.log(duration.asHours());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
In your code sample, you are parsing the input string as local date and then converting it to UTC (with utc() method).
See Local vs UTC vs Offset guide to get more info.

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.

Format date in JS using Moment.js

There is the following code:
console.log(order.start_time);
console.log(moment(order.start_time).format("HH:MM"));
I just want to get hour and minute from date using moment and display it. Output:
2014-06-30T09:00:00.000Z
13:06
But I don't understand why my date is formatted incorrectly. Thanks in advance.
Please note that there is a difference between MM and mm. The first formatting option is used for months, while the second is for days.
More information here: http://momentjs.com/docs/#/parsing/string-format/
From the doc here:
http://momentjs.com/docs/#/displaying/format/
You should use HH:mm. Then your time will have right format.
Further information, the time might be incorrectly as you expected because timezone. Momentjs auto display time in your system timezone.
Update:
To display time in specified Timezone, use "zone" method:
console.log(moment(order.start_time).zone('+00').format("HH:mm"));

Bootstrap datetime picker picking day before selected

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.

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