How to get the date object - javascript

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

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', '');

How to get date format in Angular-JS

My requirement is something different. I want to get the date format, not to format the date. Means I have a date string and now I want to get the date format of that date and apply it to the another date as a format.
Let me explain in brief with example:
var dateStr = "2015-06-06T12:00:00Z";
var d = new Date(dateStr);
here my date format is yyyy-MM-ddTHH:mm:ssZ you can see in dateStr object.
Now i will create another date and want to apply the same date-format to this new date.
var formatStr = "yyyy-MM-dd'T'HH:mm:ss'Z'"; // want to get this from above date, not hard coded like this.
var newDate = $filter('date')(d, formatStr);
here you can see that i have hard coded the format string, which i don't want to do. Here this string should be come from the above d date/or dateStr String.
You can do it by using momment.js
http://momentjs.com/downloads/moment.js
van date=new Date(date);
var dateInFormate=moment(date);
var date=dateInFormate.format('yyyy-MM-ddTHH:mm:ssZ');
As #Rob said, there is doubt on the reliably for all formats. What you need is pre defined map with key being the format and value being its corresponding regular expression.
Now, create a function with input as dateStr and will return the format. Like
function getDateFormat(dateStr) {
var format = default_format;
// Check in map for format
// If you get the format in map, return that else return a default format.
return format;
}

Javascript datetime string to Date object

I am debugging a small application with some functionality which would only run in Chrome. The problem lies in a datepicker where you choose a date and time and the datepicker concaternates it into a datetime-string.
Anyway the string looks like this: 2012-10-20 00:00.
However, the javascript that uses it now just takes the string and initialize an object with it like this: new Date('2012-10-20 00:00');
This is resulting in an invalid date in Firefox, IE and probably all browsers but Chrome.
I need advise in how I best could transform this datestring to a Date object in javascript. I have jQuery enabled.
Thanks for your sage advise and better wisdom.
If the string format is always as you state, then split the string and use the bits, e.g.:
var s = '2012-10-20 00:00';
var bits = s.split(/\D/);
var date = new Date(bits[0], --bits[1], bits[2], bits[3], bits[4]);
It's just the simplify version:
var newDate = new Date('2015-04-07 01:00:00'.split(' ')[0]);
if str = '2012-10-20 00:00'
new Date(str.split(' ')[0].split('-').join(',') + ',' + str.split(' ')[1].
split('-').join(','))
should do the trick
use parseExact method
var date = new Date.parseExact(dateString, "yyyy-mm-dd hh-mm");

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