on the aspx I am getting
date = /Date(1420460565000)/
I tried to parse it javascript date bject
var dateformatted = new Date(date);
However when I run it I am getting Invalid Data
How do I parse the c# DateTime object?
You could try this one:
var dateformatted = new Date(parseInt(date.substr(6)));
This works because substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor. Hence a new Date can be created.
Related
I'm trying to parse epoch time in milliseconds to UTC timestamp. To achieve this I'm doing:
new Date([my variable in milliseconds]).toUTCString()
and this is returning the following:
output
When the correct date should actually be:
correct date
To recreate this you can use this code:
let dateInMills = 1655154491.8357913;
let parsedDate = new Date(1655154491.8357913).toUTCString();
console.log(parsedDate);
The number that I'm passing to the Date object is the exact same number that is being passed during the tests as it is static data.
More proof on the error:
running the code in the browser console
The Date constructor can take a number as it's parameter. However, this number should be in milliseconds. In your case, you seams to have the time in second. You should multiply your input by 1000.
let dateInMills = 1655154491.8357913;
let parsedDate = new Date(dateInMills * 1000).toUTCString();
console.log(parsedDate);
I would like to convert a string value that I fetch from my database and convert it to a date variable in the view using javascript
I have tried out the following code;
var date = moment({!! $cheque->cheque_date !!}).format('YYYY-MM-DD');
I am getting the following result when I console log the date variable.
1970-01-01
you need to add " quotations
checkout this
var date = moment("{{ $cheque->cheque_date }}").format('YYYY-MM-DD');
this will converted to
var date = moment("2019-04-10").format('YYYY-MM-DD');
while your code will be converted to
var date = moment(2019-04-10).format('YYYY-MM-DD'); // no quotations
I am looking for a way to get minutes only from a date in string (coming from toISOString).
When using Date object, I was using getTime(), but dont think there is a direct method available for ISO format.
Would I need to extract strings directly from ISO format, as its just a string?
Code:
var depTime = new Date(1222332000).toISOString();
This gives me "1970-01-15T03:32:12.000Z", so what is a good way to get minutes which is "32".
You can use getMinutes() from the date object:
var d = new Date('1970-01-15T03:32:12.000Z');
console.log(d.getMinutes());
This is the right method, but, if there are issues with the Time Zone, you can parse the string:
var depTime = new Date(1222332000).toISOString();
console.log(depTime.split(":")[1]); // 32
You can use
deptime.getMinutes(); // 32
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes
Since it's just a string, regex should work just fine.
T\d+:(\d+)
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;
}
I am using Appcelerator Titanium, and I'm trying to parse a date string as a new Date object and then use the .getTime() function but it keeps returning "NaN"
var d = new Date("2014-02-01T00:00:00");
var time = d.getTime();
console.log(time); // returns NaN
Am I doing anything wrong here? It works when I create a new date for now, like this:
var d = new Date();
var time = d.getTime();
console.log(time); // returns correct value
I can't see why the first example is working but the second example is not.
You're trying to parse a UTC date time. In Titanium, when you try to parse the date, it will return invalid date. So you need to convert it to datetime string. You can use Convert UTC Date to datetime string Titanium to convert the time.