How to format JSON date? - javascript

so, i need format JSON date from this format
"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SS to format like this : DD/MM/YYYY, so i dont need info about hours, min and sec, and i need replace months and days from json, i tried some different ways but it always failed
i need do this using jQuery
also i didnt find any answer to formating this date type, all i found was formating date like this :/Date(1224043200000)/
so anyone have idea?

you can create a Date Object from a string like so:
var myDate = new Date(dateString);
then you can manipulate it anyway you want, one way to get your desired output is:
var output = myDate.getDate() + "\\" + (myDate.getMonth()+1) + "\\" + myDate.getFullYear();
you can find more at this elated.com article "working with dates"

Unfortunately your "from" dateformat is not the one which is implementation-independent in JavaScript. And all the other formats depends on the implementation, which means even if this format would be understood by most of the implementation I/you can't be sure for example how the DD and MM order would be parsed (I am almost sure it would be local regional settings dependent). So I would recommend to use a 3rd party (or your hand written) date parser to get a Date object out of your input string. One such parser you can find here:
http://www.mattkruse.com/javascript/date/
Because your question is not 100% clear for me, it's possible that you have your date in the format of /Date(number)/ which suggests that you are calling an ASP.Net service from your jQuery code. In this case during the JSON parse you can convert it to a Date object:
data = JSON.parse(data, function (key, value) {
// parsing MS serialized DateTime strings
if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') {
return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
// maybe new Date(parseInt(value.substr(6))) also works and it's simpler
}
return value;
});

The code below solved my problem:
var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10));
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();

Try something like this :
var date = new Date(parseInt(jsonDate.substr(6)));
where jsonDate is variable that stores your date

Related

Add time to string data/time

I am trying to pass a time stamp to my API that comes in the format of YYYY-MM-DD HH:MM:SS but i need to manipulate the time to add 5 hours.
Have I done something wrong here? Do I need to convert it to a JavaScript date first?
var manDate = "2020-08-16 16:15:00"
manDate.setHours(manDate.getHours() + 5);
data.manDate = manDate
console.log(manDate)
Expected output - 2020-08-16 21:15:00
When you create a var for date, you need to add the 'new Date()' method.
var manDate = new Date("2020-08-16 16:15:00");
manDate.setHours(manDate.getHours() + 5);
console.log(manDate.getHours());
And to log the hours use getHour() again in the log statement.
Use simpleDateFormat to format the date, then cast the formatted date to calendar and add hours to it.
Try with below code.
SimpleDateFormat sdfObj = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
sdfObj.parse("2020-08-16 16:15:00");
Calendar calendar = sdfObj.getCalendar();
calendar.add(Calendar.HOUR_OF_DAY, 5);
The question was asked to give a solution in java earlier. Below is the answer as per java.
Date newDate = DateUtils.addHours(oldDate, 5);

How to add days to current date in angular

Using this:
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
I get a date and time like this 2018-12-30T14:15:08.226Z, but only i want is this 2018-12-30. How can I retrieve just the date?
**This is Fixed. Thank You everyone who helps!!!
You're experiencing a JS problem, it has nothing to do with Angular.
This will use Date methods to get all the data you want:
const date = new Date ();
let dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
// 2018-12-26
You can take advantage of the fact that the ISO 8601 format (what you are getting by implicitely converting to string) is well-codified with a separator T between the date and time in order to split it.
toISOString() gives you what you're seeing. split("T") splits the string into an array of strings with T as separator. [0] then extracts the first element.
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
console.log(myDate.toISOString().split("T")[0]);

How to do a date format in Javascript?

I need to understand how to do date formattting in javascript.
i have date as,
var date="12/02/1994";// dd/mm/yyy
var date1=new Date(date);
date1.getDate();// this gives me Month which is 02
date1.getMonth();// this gives me date which is 12.
How do i get the exact date i have in var date in get date and getmonth function? Please help
The answer is pretty simple: JavaScript uses mm/dd/yyyy data format.
It doesn't support dd/mm/yyyy format, so, if you need to parse this format, then you will have to do this manually like this:
function parseDdmmyyyy(str)
{
var spl = str.split('/');
return new Date(spl[2], spl[1] - 1, spl[0]);
}
or you will have to use external libraries like Moment.js.
Javascript date() expects date in mm/dd/yy and not in dd/mm/yy. And months start from 0 and not 1.
var from = "12/02/1994".split("/");
var date1 = new Date(from[2], from[1] - 1, from[0]);
date1.getDate();
date1.getMonth();
Use new Date('02/12/1994'), new Date('1994-02-12') or new Date(1994, 02-1, 12), because in js months start from 0 and american date format is used where month goes first
you can use the simple JS file DateFormat.js which has some very good example through the URL mattkruse (Date Funtion)
from this JS file you can validate the incoming date is a true format even you can add format date within a several ways.
Presumably you want to know how to format strings so they are consistently parsed by browsers. The short answer, is there is no guarantee that any particular string will be correctly parsed by all browsers in use (or perhaps even most).
So the bottom line is: don't parse strings with the Date constructor, ever. It's largely implementation dependent and even the one format specified in ES5 and ECMAScript 2015 is poorly and inconsistently supported.
How browsers treat a string like "12/02/1994" is entirely implementation dependent, however most will treat it as the peculiar US month/day/year format, i.e. 2 December and getMonth will return 11, since months are zero indexed.
So you should always manually parse strings (a library can help, but a simple parsing function is only 2 lines, 3 if validation is required), e.g.
// Parse a date string as d/m/y
// If s is not a valid date, return a Date object with its
// time value set to NaN.
function parseDMY(s) {
var b = s.split(/\D/);
var d = new Date(b[2], --b[1], b[0]);
return d && b[1] == d.getMonth()? d : new Date(NaN);
}
document.write(parseDMY('12/02/1994'));

How to Convert date string to json date format in javascript?

In my application I am getting date in a string format like :
var date="1988-11-4".
I am calling back the WCF service and sending data to the service as Json format. But my problem is the WCF service is only accepting the dates as {DoB:"/Date(570931200000+0530)/"} format.
can you please tell how do I convert date to json date format like:
var jasonDate="/Date(570931200000+0530)/". Where 570931200000 is the miliseconds calculated since from "1970-01-01" and +0530 is the Timezone.
As a best guess, and to give you something to work with, until you understand what the relationship is and come back and explain things better along with what you have tried and the precise nature of the problem with your code.
var dateTime = '1988-05-03',
parts = dateTime.split('-'),
date;
parts[1] -= 1;
date = new Date(Date.UTC.apply(null, parts));
document.body.textContent = '/Date(' + date.getTime() + '-0000)/';
This might work:
var jsonDate = new Date(date).toJSON();
As the initial variable is only a string it would not be recognised as a date so create a date from it then convert that to JSON.
Thank you all for your response. I have got solution to my query. Here in the string "/Date(1208559600000-0700)/" 1208559600000 is the milliseconds calculated since from Jan 01 1970 and -700 is the time zone.
This the code that worked for me:
convertToJsonDate: function (date) {
var diff = date.getTime();
var jsonDate = "\/Date(" + diff + "-0700)\/";
return jsonDate;
},

Converting Javascript String to Datetime

I have a text box that is populated from a datepicker in Javascript. The date is stored as 30-Jan-2013. I want to convert this to a date so I can use it in other calculations.
I have tried
var date1 = new Date(document.getElementById('textbox').value)
but this returns Nan
if I drop the new Date part e.g.
var date1 = (document.getElementById('textbox').value
I get the date 30-Jan-2013 I just don't seem to be able to convert this?
Looks like Date.parse is not going to work because the format the datepicker is returning.
You might want to look at the following thread for parsing solutions or change your format that the datepicker outputs to one of the supported formats.
How can I convert string to datetime with format specification in JavaScript?
http://blog.dygraphs.com/2012/03/javascript-and-dates-what-mess.html
Useful parse info:
http://msdn.microsoft.com/en-us/library/ie/ff743760(v=vs.94).aspx
I'd like to recommend you a simple, easy to use and fast library [moment.js][1]
moment("30-Jan-2013").format('MMMM Do YYYY, h:mm:ss a');
will return "January 30th 2013, 12:00:00 am"
Hope this will work for your problem
var date= document.getElementById('textbox').value.split("-");;
var date1 = new Date(date[2] + " " + date[1] + " " + date[0]);
date1 .toLocaleDateString("en-GB");

Categories