How should I instantiate this date in javascript? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Format Date in Javascript
I have this date:
var newDate = "/Date(1333609200000)/";
And what I need to do to it is:
var myDate = new Date(1333609200000);
I thought about just trimming off "/Date()/" and then getting just the number inside but I wasn't sure if that would be best here. Should I just simply regex the number out of that? Is there a better approach?
Notes: The date is being created by c#'s JavascriptSerializer and comes in the format newDate above. It cannot be changed in c# due to constraints.

You could do it like this:
var newDate = "/Date(1333609200000)/";
newDate= new Date(parseInt(newDate.match(/\d/g).join("")));

Related

How to add hours in datetime in JavaScript [duplicate]

This question already has answers here:
How to add hours to a Date object?
(20 answers)
Closed 4 years ago.
I want to add hours in DateTime. I have googled it but couldn't find any code.
Here is this datetime 2018-07-25 20:23:22. I want to add hours in this datetime so it gives me new datetime in the same format
I have tried this.
var datetime = "2018-07-25 20:23:22";
datetime.setHours(datetime.getHours()+5);
But It didn't work.
Your datetime is String. You need to convert it into Date object first.
var datetime = new Date("2018-07-25 20:23:22");
console.log("Before: ", datetime);
datetime.setHours(datetime.getHours()+1);
console.log("After: ", datetime);
I would prefer to use moment.js library for manipulating and playing with dates.
Hope this may help you.
Here is the simple way with using Moment.js
const date = moment('2018-07-25 20:23:22');
date.add(5, 'h');
console.log(date.toString());

Javascript Determine 10 days before specific date [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 4 years ago.
So I am currently fetching some records from my database, and one of the records is a 'last edited' which is a date. However, I just want to display this 10 days before it has been exactly one year since the specific date. How would I approach this? I know how to deal with arrays and the fetching itself, but I'm not sure how I would deal with the date?
This is what I've done so far:
var startDate = fetchedDate;
var validDate = new Date(startDate);
var fullYear = validDate.getFullYear();
validDate.setFullYear(fullYear + 1);
And then I need to create a new date to compare it, I suppose? But how? I also want to know how many days it is until it has been one year.
If you wanna make it with pure javascript you can
var startDate = fetchedDate;
var validDate = new Date(startDate);
validDate.setYear(validDate.getFullYear() - 1);
validDate.setDate(validDate.getDate() - 10);
But I also will recommend you to use moment.js, is a very good javascript library to handle dates, otherwise sometimes is like trying to reinvent the wheel.

Javascript date to C# date in AngularJS [duplicate]

This question already has answers here:
Proper Way to Convert JSON Date to .NET DateTime During Deserialization
(6 answers)
Closed 5 years ago.
I have a date like this var jsDate = "2017-01-01";
I want to put this jsDate like C# date: (/Date(1425408717000)/)
Also how can put this c# date /Date(1425408717000)/ into JavaScript Json date?
In JavaScript you can try: (new Date("2017-01-01")).getTime() , which will give you 1483228800000 if that's what you are looking for

Calculating age in Javascript with YYYY-mm-dd format [duplicate]

This question already has answers here:
Calculate age given the birth date in the format YYYYMMDD
(45 answers)
Closed 6 years ago.
I have birthday in following format:
1982-09-20
I need to get the exact age of the person from that birthdate ( compared with the current date).
What's the easiest way to do this in JS? Can someone help me out please?
Thanks!
I assume you want the exact age in years. If you want another value, it's all in that magic number of have in the function below, a bit verbose for clarity:
var MILLISECONDS_IN_A_YEAR = 1000*60*60*24*365;
function get_age(time){
var date_array = time.split('-')
var years_elapsed = (new Date() - new Date(date_array[0],date_array[1],date_array[2]))/(MILLISECONDS_IN_A_YEAR);
return years_elapsed; }
You would call:
get_age ('1982-09-20')
This is probably going to be marked as a duplicate and deleted.
There might be a better in built way, but this should work fine for your needs.

How to change the date format of ISOString? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 7 years ago.
How can I change the output to be more readable? from this 2015-04-25T00:00:00Z to 2015-04-2 00:00:00-00:00:00
I am using this (new Date()).toISOString()
Dates are very strange beast in javascript. If at all possible I would highly recommend using a date library like momentjs to handle this for you. Using moment this would be as simple as:
var formattedDate = moment().format('YYYY-MM-DD hh:mm:ss')
This is one way to handle it in JavaScript. You get the Date and then the various parts. Then you concatenate them together into a variable to use as you wish.
var d = new Date();
var dy = d.getDate();
var dmth = d.getMonth();
var dyr = d.getFullYear();
var dFullDate = dyr+ '/' +dmth+ '/' +dy;

Categories