format just a time for use with ui-bootstrap timepicker - javascript

I have a server returning a time value like so
14:00
The reason for this is it represents the start time of say a task that happens repetively say every monday at 14:00 so the date is irrelevant. I want to display this value to the user when they are editing the task so that they can change it with ui-bootstraps timepicker.
The timepicker requires either an epoch, an rfc2822 or ISO 8601 date so I need to just add a random date that I can discard later for the timepicker to be able parse and display the value (which seems ridiculous seen as it is a time picker not a time on a specific day picker but whatever)
so either using the javascript date object or moment.js how can I take that value and create a valid date object with it?

As you requested a moment.js solution also:
var date = moment("14:00", "HH:mm").toDate();

// assume returned is the hours and minutes you get in this format: 14:00
hourData = returned.split(':');
var date = new Date();
date.setHours(hourData[0]);
date.setMinutes(hourData[1]);
date will be something like Sat Jul 12 2014 14:00:13 GMT+0300 (EEST)

Related

How can I convert GTM-0006 to ISO without Adding hours

I'm working in a from who has a date field and by default it shows the current date.
I set the date using this:
var date = new Date(); = Tue May 25 2021 17:06:01 GMT-0600 (Mountain Daylight Time) {}**
Everything works fine, but when I send the data to the controller, the JSON automatically converts it to ISO and the date received by the controller is 6 hours in advance.
I understand a little bit the context about GMT-0006 (my current timezone is 6 hours more than the 0 timezone), and the fact that my controllers received the date in ISO format because when I converted to ISO format is the same problem
date.toISOString() = "2021-05-25T23:06:01.861Z" (6 hours in advance)
so my question is, there is a way to create a date that allows me to use .toISOString() and keep the same?
or create a date with my current hour but -0000 so when I convert it to toISOString keeps the same?

moment fromNow returns in 5 hours when parsing utc

trying to format utc time from server in time ago using moment.js fromNow but in some occasions I get "in 5 hours" instead.
timestamp from a server - 2017-11-29T15:03:21
var utcTime = new Date(timestamp);
var timeAgo = moment(utcTime).fromNow();
console.log(timeAgo)
all dates are in past so how can I fix this so I dont get time in a few hours ?
If you want "2017-11-29T15:03:21" treated as UTC, you can either use moment's utc method or just append a "Z" to the string. Since you're already using moment.js, it's more reliable to parse it with moment.js than the built-in parser:
var timestamp = "2017-11-30T00:20:48";
// Append Z
console.log(moment(timestamp + 'Z').fromNow());
// Use .utc
console.log(moment.utc(timestamp).fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
You need to tell moment that this date is in UTC using moment.utc
var utcTime = new Date(timestamp);
var timeAgo = moment.utc(utcTime).fromNow();
If you don't, moment assumes this date is in your local timezone (which I can tell is Eastern Standard Time by the offset).
In your local timezone, this date is actually 5 hours in the future. Only in UTC is it a few seconds ago, because your local timezone is 5 hours behind UTC.
As per documents https://momentjs.com/docs/#/displaying/fromnow/
you can customize the locale https://momentjs.com/docs/#/customization/relative-time/
As default locale future time will be future: "in %s", having in which is as per documents. if you want to change it then update the locale and use as you want.
Hope this helps

MongoDB + JS Date shifted -1 day

I'm trying to add a new date inside my mongodb document using
db.collection('course').insertOne({
date: new Date(item.date)
});
item.date is from
items.push({
date: moment(new Date(day.label)).tz('Europe/Paris').format('YYYY/MM/DD'),
});
day.label is like '14 November 2016'
item.date is formated like 2016/11/14
The document insert work but the dates are not properly formated, for example, for 2016/11/14 Mongo insert "date" : ISODate("2016-11-13T23:00:00Z")
Every inserted dates are shifted -1 day
Is the proper way to insert custom formated date in MongoDB ?
Should I change the momentJs date format ?
EDIT:
Dates are shifted because of different timezone.
It's not shifted by a day, it's not actually shifted at all.
The time shown is shifted by an hour. If you notice, the time has become 23:00 on the previous day, rather than defaulting to midnight on the date you have specified.
More importantly, the Z suffix in the timestamp means that the timezone is UTC, and 2016-11-13T23:00:00 in UTC refers to the same moment in time as the timestamp 2016-11-14T00:00:00+01:00. So the datetime is being created correctly, but just shown back to you in a different timezone that you're creating it in.
if you want proper solution for all date UTC problem, store
date as standard ISO format
// India
moment().toISOString() // "2016-11-06T06:17:33.520Z"
// canada
moment().toISOString() // "2016-11-06T06:19:42.133Z"
now see the diffrence by convering date from database
// canada time zone
moment("2016-11-06T06:19:42.133Z").format() // "2016-11-05T23:19:42-07:00"
// india time zone
moment("2016-11-06T06:19:42.133Z").format() //"2016-11-06T11:49:42+05:30"
as above see the diffrence of local UTC, so don't have to convert date from any
timezone to other timezone

Stop Javascript DateTime adjusting to local time zone

I am receiving the correct unix time that I use in my html page in my javascript. However, when I view or alert the unixtime, what happens is that the date is adjusted based on my desktop's current timezone, but I need the DateTime to be without the timezone.
This is how I pass the data in my javascript, one of the many ways I use the unixtime:
<script type="text/javascript">
$(document).ready(function(){
var a = <?php echo "1434203820"; // an example only?>;
var date = new Date(a);
alert(date);
});
</script>
what the unix time should show is:
Jun 13 2015 13:57 (if it was converted)
but it is showing this date:
Jun 13 2015 21:57 malay peninsula time
What basically happens is that the javascript gets my local time zone and converts that date to that time zone. How could I force it(and all other javascript functions that I use) to use the original time it supposed to be showing?
If you want the UTC format you can use one of the many UTC methods. For instance you could use Date.prototype.toUTCString:
var unixTS = 1434203820 * 1000; // JS date is in millseconds
var date = new Date(unixTS);
document.write(date.toUTCString());
Maybe what you want is converting back the timestamp to UTC format ?
How do you convert a Javascript timestamp into UTC format?

DatePicker Error in Angular JS due to UTC

I have selected the 12th of September 2014 in the UI.
Following is the code
ctrl.$formatters.push(function (modelValue) {
console.log(modelValue);
var dt = new Date(modelValue);
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
console.log(dt)
return dt;
});
The two console logs i see are the following.
09/11/2014
Wed Sep 10 2014 18:30:00 GMT+0530 (IST)
Am not sure why the conversion from UTC to local is not carried out correctly.
Thanks in advance.
Its not clear what you are trying to do. The input does not have a time. Do you want to add the current time of the day to the arbitrary date? Or do you just want a local representation of the date? I'm geussing the latter.
Instead of dt.setMinutes(...) and the following two lines, replace all three lines with:
return dt.toLocaleDateString();
If you ARE trying to set the time to now on whatever date is input (I don't know why...) but you might try:
dt.setTime( new Date().getTime() );
instead of the setMinutes(...) line.
then you can
return dt.toLocaleString;
All date objects are stored as miliseconds since, like 1972. It is best to use the built in Date object methods to get what you want from it. Here are the docs for reference.

Categories