Javascript string to date - javascript

I have this string in Javascript
"/Date(1317772800000)/"
and I'd like to display it as a meaningful date in my page. Currently, when I output the variable that contains this date I get the following displayed on my page
/Date(1317772800000)/
What I'd like is to display this in the format DD MM YYYY like so
10 05 2011
How is this possible?

try this
var date = new Date(Number.parseFloat('/Date(1317772800000)/'.substring(6)));
var newdate = date.getMonth() +' ' +date.getDate() +' ' +date.getFullYear()

If you have your date in a string provided then first you need to extract the number:
var strDate = "/Date(1317772800000)/";
var dateInt = strDate.replace("/Date(","").replace(")/","");
var date = new Date(parseInt(dateInt))
This gives you a JavaScript date object that you can do pretty much a lot with, if you want simple check just execute:
alert(date)

Try using moment.js i.e.:
moment().format('MMMM Do YYYY, h:mm:ss a')
Then you can do:
moment(1317772800000).format("MMM Do YY");

Try this
unixtime = 1317772800000;
var newDate = new Date();
newDate.setTime(unixtime);
dateString = newDate.toUTCString();
alert(dateString);
DEMO

Related

New date() returning next date instead of today's date

i am trying to convert a string into date type.i am giving the string value to new date().
but it's returning next day date instead of date which i am trying to convert.
var endDate = new Date("2017-03-23T23:59:59.000Z");
//end date value is now ------ Fri Mar 24 2017 05:29:59 GMT+0530 (India Standard Time).
Please suggest me how can get correct date in the format MM/DD/YYYY
This hack can help you,
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
it will give you,
"2017-03-23T23:59:59.000Z"
Further if you want to convert it to DD/MM/YYYY then you can use native javascript or lib like moment for that,
This simpile js will help to convert it to any format.
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
var d1 = endDate.split('T'); //spliting date from T
var d2 = d1[0].split('-'); //getting date part
console.log('yyyy/MM/dd', d2[0] + "/" + d2[1] + "/" + d2[2]) //YYYY/MM/DD
console.log("DD/MM/YYYY", d2[2] + "/" + d2[1] + "/" + d2[0])
jsfiddle link
if your time is in IST use below
var endDate = new Date("2017-03-23T23:59:59.00+0530");
If you check dates, you will see that your dates differs in 5h 30 mins, that is same as your date saying GMT +0530. Your original date has .000Z that is time zone of GMT +0.
Make sure you use same time zone when working with date.
Try using Date.UTC('your date')
JavaScript Date objects carry no timezone information. The only reason you saw a non-UTC date is that the browser chooses by default to display dates as local time in the console. If you don't care about the date object aligning with the exact instant in local time, you can use the following format function to turn it into MM/DD/YYYY format:
function format (date) {
var mm = ('0' + (date.getUTCMonth() + 1)).slice(-2)
var dd = ('0' + date.getUTCDate()).slice(-2)
var yyyy = date.getUTCFullYear()
return mm + '/' + dd + '/' + yyyy
}
var endDate = new Date("2017-03-23T23:59:59.000Z")
console.log(endDate.toISOString())
console.log(format(endDate))
(Credit to Ranj for posting an answer using Date#toISOString before mine.)
I have created the solution over here please find below link
https://www.w3schools.com/code/tryit.asp?filename=FD0YSGRMB59W

JavaScript date format convert.

Date from datetimepicker have format:
var currentDate = new Date(); currentDate = Thu Jul 14 2016 09:10:04 GMT+0200 (Środkowoeuropejski czas letni) {}
And this date have methods like .getFullYear() etc.
But when I send it to my API where this date is DateTime and send back it to frontend it look that 2016-07-22T22:00:00Z and it doesn't have methods like .getFullYear() etc.
That is problem for me. I need detect if the date is formatted yyyy-mm-ddThh-mm-ssZ and convert it to the first format.
How I can do it? I can't use momentjs.
format your date string in javascript
var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var year = date.getFullYear();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var datetime= day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;
Then in c#
DateTime.ParseExact(DatetimeString , "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Why you do not use this:
http://momentjs.com/
this allows all sorts if formatting and its safer to use due to incorrect dates possible
var a = moment('2016-01-01');
var b = a.add(1, 'week');
a.format();
"2016-01-08T00:00:00-06:00"
moment().format("MMM Do YY"); // Jul 14th 16
Use standard format yyyy-mm-dd hh-mm-ss
You're possibly receiving a string from your API. So you have to parse it back to Date object
var newDateObject = new Date(dateStringFromAPI);
Then you can access newDateObject.getDay() newDateObject.getFullYear() etc
as you are using AngularJS you can simply format date with Angular's date filter https://docs.angularjs.org/api/ng/filter/date
Good day to you!
Looks like the front-end doesn't parse the API-dataset to create a date-object (no date-object—no date-methods)—here is a solution:
Enable date-parsing on the front-end;
If needed, apply a date-time format on API output (MSDN-articles: Date and Time Format Strings—Standard and Custom);
Send the API-processed data to the front-end.

How To Convert String ' dd/mm/yy hh:MM:ss ' to Date in javascript?

I have Done this
var d='dd/mm/yy hh:MM:ss';
var d1=d.split(" ");
var date=d1[0].split("/");
var time=d1[1].split(":");
var dd=date[0];
var mm=date[1]-1;
var yy=date[2];
var hh=time[0];
var min=time[1];
var ss=time[2];
var fromdt= new Date("20"+yy,mm-1,dd,hh,min,ss);
Is there Any way to do it using JQuery OR JavaScript?
If you are looking for alternatives in jquery or Javascript , then you can go with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.
example:
var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');
This will work regardless of timezone for the format dd/mm/yyy hh:mm:ss only. It also does not rely on third party packages:
let dtStr = "12/03/2010 09:55:35"
console.log(strToDate(dtStr)); // Fri Mar 12 2010 09:55:35
function strToDate(dtStr) {
if (!dtStr) return null
let dateParts = dtStr.split("/");
let timeParts = dateParts[2].split(" ")[1].split(":");
dateParts[2] = dateParts[2].split(" ")[0];
// month is 0-based, that's why we need dataParts[1] - 1
return dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1], timeParts[2]);
}
How about Date.parse()?
new Date( Date.parse("05/12/05 11:11:11") );
// Thu May 12 2005 11:11:11 GMT+0200 (CEST)
The output produced is in local timezone and will differ in browsers in different timezones.
We can convert any local date format to datetime datatype using moment js.
Syntax:
moment('<local date value>','<local date format>').format('<expected convert format>')
Example:
moment('26/05/1986 00:00', 'DD/MM/YYYY HH:mm').format("MM/DD/YYYY HH:mm");
then the output will be 05/26/1986 00:00
Cheers
Date#parse should be able to parse that if you split on a string. Id also recommend looking into the npm package moment for date manipulation
From your code it seems that you are trying to convert string into date and also you are trying to fetch previous month. If yes then you can reconstruct your code as below:
Date.prototype.SubtractMonth = function(numberOfMonths) {
var d = this;
d.setMonth(d.getMonth() - numberOfMonths);
return d;
}
$(document).ready(function() {
var dateString='2015-06-17T18:30:12';
var d = new Date(dateString);
alert(d.SubtractMonth(1));
});

converting a date to european format

I am writing a script for automating XML data in Indesign. I currently have in the XML the date in American Format (MM/D/YY) but once I run the script in Indesign my goal is to have it be in European format (DD/M/YY). What can I add to my script in order to write a function that will convert any date formats to european formats? I hope this makes sense. I need help!
i guess simply using
function convertDate(dateString) {
var date = new Date(dateString);
return date.getDate()+"/"+(date.getMonth() + 1)+"/"+date.getFullYear();
}
just note european format does not use slashes but dots as far as i know so it should look like this
dd.mm.yyyy
not
dd/mm/yyyy
becouse it can be mismatched with us format
How to format a JavaScript date?
Return dd-mm-yyyy from Date() object
var d = new Date().toLocaleDateString("uk-Uk");
Output: 03.09.2021
var d = new Date().toLocaleDateString("en-GB");
Output: 03/09/2021
var d = new Date().toLocaleDateString("en-UK").replace(/\//g, '-');
Output: 03-09-2021
var myDate = new Date('myDateString'); //you can also do milliseconds instead of the date string
var myEuroDate = myDate.getDate() + '/' + myDate.getMonth + '/' + myDate.getFullYear();
Useful as well...
http://arshaw.com/xdate/

What is the best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date?

What is best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date format.
var from_date = document.getElementById('from_date').value;
var YYYY = from_date.substring(0,4);
var MM = from_date.substring(4,7);
var DD = from_date.substring(7,8);
var myDate = new Date( parseInt(YYYY,10), parseInt(MM,10)-1, parseInt(DD,10) );
note that the month provided to the date constructor is actual month number - 1.
edits: ok, there are some issues with your date part extraction- substring is probably the most awkward of javascript's sub-stringing methods (sub,substr,substring). And after testing I stand by the month value having to be 1 less than the actual number. Here is a fixed sample.
var from_date = "20101127"; //document.getElementById('from_date').value;
var YYYY = from_date.substring(0, 4);
var MM = from_date.substring(4, 6);
var DD = from_date.substring(6);
var myDate = new Date(parseInt(YYYY, 10), parseInt(MM, 10) - 1, parseInt(DD, 10));
alert(myDate); // should be november 27th 2010
It's easy, I struggled with the same question but came up with an easier way.
Create a hidden div tag and set the date into an attribute. then retrieve the attribute and use the sub string method to get something like Mar 01.
<div id="test" style="display:none;" date=""></div>
<script type="text/javascript">
var date = new Date();
document.getElementById('test').setAttribute('date',date);
var getdate = document.getElementById('test').getAttribute('date');
var newdate = getdate.substr(4,7);
alert(newdate);
</script>
That's all thanks!
check out my operating system: hypertos(dot)webege(dot)com

Categories

Resources