Typescript - Removing milliseconds - javascript

In typescript, I am setting an object's date field in following way.
at: new Date("2017-06-24"+"T"+"22:00"+"Z")
Then I am sending that object as POST body to my webservice. In body, date field looks like "at":"2017-06-24T22:00:00.000Z"
But I want o get rid of milliseconds part. How can I do that ?

You can format the String you have from the form input values as
new Date("2017-06-24"+"T"+"22:00"+"Z").toISOString().split('.')[0]+"Z"
else you can directly generate the String from your form input values
`${this.form.value.Date}T${this.form.value.Time}Z`
in both the above cases you have the String output as 2017-06-24T22:00:00Z
and not the Date type.
Date constructor in Js will have the milliseconds field
so it will be an issue if at in your Object is defined as a Date instead of a String.

Related

Is there a way to add the current day's date within an email body in dd/mm/yyyy format?

Where I work there is the need to send emails where we need to add a specific code and then the current date on a new line, I have been able to sort out the first aspect using the following:
<button onclick="location.href='mailto:Kofax.AriesNotes#xxxxbank.co.uk?body=NB%0d%0aHMLR%0d%0a';">HMLR</button>
On the third line I want the date to pre fill in dd/mm/yyyy format but am having difficulty working out how to do this.
Instead of inlining the event handler, make a separate function (say buttonClicked) and attach it to onclick. Inside the buttonClicked method, use the Date() constructor to get a date object. Then using the getDate(), getMonth(), and getFullYear() functions, build a date variable in the format you desire. Afterwards, do a string concatenation to add that to the mailto: URL string, and finally point location.href to that.

Convert database date to short date with Javascript

I have a SQL lookup a date that feeds into a field, but the date format contains time, I need to convert it to short date (mm/dd/yyyy). The MSSQL outputs this format (m/d/yyyy 12:00:00 AM) notice that the time is always '12:00:00 AM'. How do I remove the time?
$('#q60').change(function () {
var date = $('#q60 input').val(); //looking up the field that contains the date fed from SQL.
date = date.substring(0, date.indexOf(' '));
});
I have tried using split but while it output the correct thing it doesn't actually change the value in the field for some reason. I have also attempted using the .format similar to this post: Format a date string in javascript
But I am stuck!
with date = date.substring(0, date.indexOf(' ')); you're just storing splitted value in to date variable. to change the value of the input field add $('#q60 input').val(date) at the end of your function.
also in JS there's a whole Date object, with it you can format your date as you please. you can find more about it here and here

date string doesn't show up in date input

I have saved date as a string.
for example:
I have object in angularJs called myBirthday.
myBirthday.date="1996-04-04";
and I want to show the date in input in the modal:
<input type="date" ng-model="myBirthday.date" />
The problem is that it's shown dd-mm-yyyy instead of 04-04-1996.
Then When I change the date,close modal and then reopen it,The input shows the correct date.
How can I make it to show the correct date? not dd-mm-yyyy?
I have tried almost every string types of date.
please HELP
You shoud create a Date object instead of a String.
myBirthday.date = new Date(1996, 3, 4);
You could also cast the Date to a String:
date_string = myBirthday.date.toISOString();
It seems date filter called before binding of date or date-format values. date/dateformat is null initially but filter called that's why filter would be giving default format string. And once values are binded properly then everything works perfectly fine

Data format - JSON

I have a JSON output from a non-configurable system where various date/time variables have values like \/Date(1422691756316)\/
How can I get that into a readable format (I need to display it via PHP and Javascripts).
You could deserialize the JSON output via json_decode() and then you could try to parse the string to an timestamp with strtotime (http://php.net/manual/de/function.strtotime.php), then you could create a date from thins timestamp using the date() method (http://php.net/manual/de/function.date.php) with a given formatting string an the timestamp.
But I'm not sure if your date string is ready to be parsed. In all cases you may to remove the \/Date( and )\/ parts of the string.
EDIT: Looks like your string is not a timestamp, strtotime fails. You may have to check what kind of timestamp/ datestamp/ whatever your string is.

new Date(milliseconds) returns Invalid date

I am trying to convert milliseconds to a date using the javascript using:
new Date(Milliseconds);
constructor, but when I give it a milliseconds value of say 1372439683000 it returns invalid date. If I go to a site that converts milliseconds to date it returns the correct date.
Any ideas why?
You're not using a number, you're using a string that looks like a number. According to MDN, when you pass a string into Date, it expects
a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).
An example of such a string is "December 17, 1995 03:24:00", but you're passing in a string that looks like "1372439683000", which is not able to be parsed.
Convert Milliseconds to a number using parseInt, or a unary +:
new Date(+Milliseconds);
new Date(parseInt(Milliseconds,10));
The Date function is case-sensitive:
new Date(Milliseconds);
instead of this
new date(Milliseconds);
use this
new Date(Milliseconds);
your statement will give you date is not defined error
Is important to note that the timestamp parameter MUST be a number, it cannot be a string.
new Date(1631793000000).toLocaleString('en-GB', { timeZone: 'America/Argentina/Buenos_Aires' });
// Returns: '16/09/2021, 08:50:00'
new Date("1631793000000").toLocaleString('en-GB', { timeZone: 'America/Argentina/Buenos_Aires' });
// Returns: 'Invalid Date'
In case you're receiving the timestamp as string, you can simply wrap it around parseInt(): parseInt(your_ts_string)
I was getting this error due to a different reason.
I read a key from redis whose value is a json.
client.get(someid, function(error, somevalue){});
Now i was trying to access the fields inside somevalue (which is a string), like somevalue.start_time, without parsing to JSON object.
This was returning "undefined" which if passed to Date constructor, new Date(somevalue.start_time) returns "Invalid date".
So first using JSON.parse(somevalue) to get JSON object before accessing fields inside the json solved the problem.

Categories

Resources