How to convert html5 input time to a timestamp in JavaScript? - javascript

Let's say I have 2:12 PM as my time from an input, and I want to convert it to a timestamp combined with the current date.
I can get the current timestamp using Date.now() but my problem is I want the time to be based on the input.
If moment can be used, then better. Any help would be much appreciated.

You could pass in the time format to moment's constructor, along with the input's value as string to parse it into a moment object. Like this:
console.log(moment('2:12 PM',"hh:mm a").format('lll'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

You should be able to use momentJS custom parsing:
moment("2:12 PM", "H:mm A");

Using simple JavaScript
Take date
var d = new Date();
Split it
var l = d.toString().split(":");
Slice it
var f =l[0].slice(0,-2);
Get your time variable
var ty="11:22:00";
Create Date
var j = new Date(f + ty);
Done, It's in j
One line solution:
var d = new Date();
var ty = "11:22:00";
var newDate = new Date(d.toString().split(":")[0].slice(0,-2) + ty);
It's the full date, you can use and change it as you like.

Related

Get "Day" From This Formatted Timestamp In Javascript

I'm working with Javascript within Google Sheets, and I'm having trouble converting or parsing a formatted timestamp, to ultimately extract the day as a numerical value.
My code:
var shopifyTimestamp = "2019-05-18 13:21:17 +0100";
var date = new Date(shopifyTimestamp);
Logger.log(date.getDay());
The output:
[19-06-10 17:40:56:107 BST] NaN
My goal is to extract the day number, for example, "18" from that timestamp.
However, it doesn't seem to convert it. I suspect my timestamp isn't in the correct format for the date() function, so it's about creating a function to parse it.
Hopefully, you can help me with that! :) Thank you so much.
The date object has a method like this for getting the day of the month as a number (1-31).
date.getDate();
18 is date.
var shopifyTimestamp ="2019-05-18 13:21:17 +0100";
var date = new Date(shopifyTimestamp);
console.log(date.getDate());
JavaScript's Date constructor supports ISO 8601 date strings. Without using any libraries, you can do something like this:
var shopifyTimestamp = "2019-05-18 13:21:17 +0100";
// will produce `2019-05-18T13:21:17+0100`
var isoDate = shopifyTimestamp.slice(0, 10)
+ 'T' + shopifyTimestamp.slice(11, 19)
+ shopifyTimestamp.slice(20);
var date = new Date(isoDate);
console.log(date.getDate()); // 18
Also note that you're looking for date.getDate(), rather than date.getDay(). The latter returns the numerical date of the week.

How to remove the letter "Z" from the end of a dateTime

I am trying to remove the Z from the end of a dateTime entity. The timezone of the API gets confused from the site I'm pushing the date from. Does anyone know a script that can remove the Z when a user types in a dateTime?
You're using UTC Date i'm guessing. You can try to use .toLocaleString() on your date time object.
Example
var datetime = new Date();
var now = datetime.toLocaleString();
This should get you something like this: 6/30/2017, 8:47:15 AM
Another option if you want to maintain the format and just remove the T and Z characters is to replace the strings. Example:
var datetime = new Date();
var now = datetime.toISOString().replace('Z', '').replace('T', '');

Modifying an ISO Date in Javascript

I'm creating several ISO dates in a Javascript program with the following command:
var isodate = new Date().toISOString()
which returns dates in the format of "2014-05-15T16:55:56.730Z". I need to subtract 5 hours from each of these dates. The above date would then be formatted as "2014-05-15T11:55:56.730Z"
I know this is hacky but would very much appreciate a quick fix.
One solution would be to modify the date before you turn it into a string.
var date = new Date();
date.setHours(date.getHours() - 5);
// now you can get the string
var isodate = date.toISOString();
For a more complete and robust date management I recommend checking out momentjs.
do you have to subtract the hours from a string?
If not then:
var date= new Date();
date.setHours(isodate.getHours() - 5);
var isoDate = new Date().toISOString();
If you do have to use a string I'd still be tempted to do:
var date= new Date("2014-05-15T16:55:56.730Z");
date.setHours(isodate.getHours() - 5);
var isoDate = new Date().toISOString();
The way to modify Date objects is via functions such as Date.setHours, regardless of the format you then use to display the date. This should work:
var theDate = new Date();
theDate.setHours(theDate.getHours() - 5);
var isodate = theDate.toISOString();
For complex date operations and enhanced browser compatibility, I highly recommend using moment.js, especially if you're going to be doing several. Example:
var fiveHoursAgo = moment().subtract( 5, 'hours' ).toISOString();
Moment.js is perfect for this. You can do:
var date = new Date();
var newDate = moment(date).subtract('hours', 5)._d //to get the date object
var isoDate = newDate.toISOString();

How to get the date object

My value contains "08.07.1987", how to retrieve the date object for this string. new Date(val) gives correct date object values only for the string value that contains "/" format. can any one let me know hot to create date object for the values which contains "." or "-". in its format.
How about just adjusting the string to suit your needs?
var date1 = new Date("08.07.1987".replace('.','/'));
var date2 = new Date("08-07-1987".replace('-','/'));
You will need to be careful when asking Javascript to interpret a date in this format. As you can probably imagine, a date listed as "08.07.1987" doesn't really specify whether it's August 7th or July 8th.
In general, your best bet will be to specify a date format and parse accordingly.
you have to split the string into tokens for month date and year and then create it using JS Date API.
var date="08.07.1987";
var newDate = date.replace(/(\.|-)/g,"/"));
var dateObject = new Date(newDate);
Replace the delimiters?
var dateStr = "08.07.1987",
dateObj = new Date(dateStr.replace(/[-.]/g,"/"));
Of course you can encapsulate that in a function if need be...
try this new Date("08.07.1987".replace('.','/','g')); tested on firefox only

How to parse the year from this date string in JavaScript?

Given a date in the following string format:
2010-02-02T08:00:00Z
How to get the year with JavaScript?
It's a date, use Javascript's built in Date functions...
var d = new Date('2011-02-02T08:00:00Z');
alert(d.getFullYear());
You can simply parse the string:
var year = parseInt(dateString);
The parsing will end at the dash, as that can't be a part of an integer (except as the first character).
I would argue the proper way is
var year = (new Date('2010-02-02T08:00:00Z')).getFullYear();
or
var date = new Date('2010-02-02T08:00:00Z');
var year = date.getFullYear();
since it allows you to do other date manipulation later if you need to and will also continue to work if the date format ever changes.
UPDATED: Jason Benson pointed out that Date will parse it for you. So I removed the extraneous Date.parse calls.
var year = '2010-02-02T08:00:00Z'.substr(0,4)
...
var year = new Date('2010-02-02T08:00:00Z').getFullYear()
You can simply use -
var dateString = "2010-02-02T08:00:00Z";
var year = dateString.substr(0,4);
if the year always remain at the front positions of the year string.

Categories

Resources