convert json callback data to date - javascript

can i get help coverting this string to date format.
Here is the JSon name and value
notBefore: 1413936000000
notAfter: 1479427199000
I believe this needs to be converted to US format date mm/dd/yyyy
The callback string looks like this:
response.endpoints[0].details.cert.notBefore
response.endpoints[0].details.cert.notAfter

Just go ahead and craft up a new Date
new Date(notBefore)
If your values are indeed strings, you can parseInt() them
JSFiddle Example
If you need a simple example for mm/dd/yyyy format, here is a working fiddle to get you up and running.
var notBefore = 1413936000000;
var date = new Date(notBefore);
var formattedDate = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
With Basic Format JSFiddle

Related

Date formatting in google-script

I want to change the format to "mmm-dd-yyyy"(Nov-11-2019) using the code that I have.
Please see below code:
var timeStamp = data[i][timeStampappr];
var formatted = (timeStamp.getMonth()+1) + '/' + timeStamp.getDate() + '/' + timeStamp.getYear();
Current format is 11/11/2019 and I want it to be like Nov-11-2019
If you use the following, then if timestamp is a string it might be of a form that the date constructor will create the date properly. If it's a date the it just creates another date object with the same value. So If you're not always sure what you're going to find in your timestamp value this approach may provide you with more consistent performance. Also note the M's have to be capitalize for the Month.
Utilities.formatDate(new Date(timestamp), Session.getScriptTimeZone(), "MM-dd-yyyy");
Simple Date Formatting Reference
JavaScript Date Constructor
Great question, a straight-forward solution if you can't find a more specific function to do the job, is to create an array of the string representation of the months. That is:
var monthsAsStrings = ["Jan", "Feb", "Mar", ... ];
var formatted = monthsAsStrings[timeStamp.getMonth() + 0 ] + '-' + timeStamp.getDate() + '-' + timeStamp.getYear();
^ ^ ^
Note that array indices start at 0, while we typically call January month 1. The + 0 is not necessary but illustrates the difference between the code you posted and what you're requesting.
Additionally, the '/' is replaced with '-'.
In this way, you could check first if you have a timestamp, before formatting the date:
function myFunction(data) {
var timeStamp = data[i][timeStampappr];
// If there is no timestamp, then do nothing.
if(timestamp){
var formattedDate = Utilities.formatDate(timestamp, "GMT", "MMM-dd-yyyy");
Logger.log(formattedDate);
}
}

Transform date to DD/MM/YY

I get the date from server in such format: 2019-01-24 00:00:00
How to convert it to 24-01-2019?
I use:
new Date(dateFromServer).toLocaleDateString()
but it returns 01/24/19.
as seen here Format date to MM/dd/yyyy in JavaScript
var date = new Date(dateFromServer);
alert(date.getDate() + '-' + (date.getMonth() + 1) + '-' + date.getFullYear());
Avoid answers that suggest parsing, it doesn't make sense to parse a string to a Date and reformat it just to get back the values you started with, given the risks of using the built–in parser (e.g. in Safari, new Date('2019-01-24 00:00:00') returns an invalid Date).
To reformat a date, just split it into the parts and put it back as you want:
// #param {string} s - timestamp in format YYYY-MM-DD HH:mm:ss
function reformatDateString(s) {
var b = s.split(/\D/);
return `${b[1]}/${b[2]}/${b[0]}`
}
console.log(reformatDateString('2019-01-24 00:00:00'));

How can I format a date string coming from the backend?

I am using Reactjs and ES6 in the frontend. And GraphQL to make some API calls.
I am getting a key named createDate with a value like this:
2017-03-29T07:19:05-07:00
And I need to format it like this:
03/29/2017 07:19 AM it should show AM or PM.
As this is an string and I am not using any library, I expect someone could guide me to a solution...
You can convert the String to a date object first
var d = new Date("2015-03-25T12:00:00-06:30");
Then define a function constructs the string you want to use for representing the date. For example
function dateToString(d) {
var year = d.getFullYear();
var month = d.getMonth() + 1; // getMonth() returns 0-11
.
.
.
return year + "-" + month "-" + ...;
}

Parse date in javascript issues

I get such date in javascript
var val = "1960-05-15T20:00:00"
But if I do
var date = new Date(val);
The data I get is one day later:
1960-05-16 // I use this to obtain it: Ext.Date.format(new Date(val), 'm/d/Y')
Can you help me how to parse this date? and get correct date with 1960-05-15?
Your date format is ISO 8601 represented as the local time with an offset to UTC appended.
The Ext.Date singleton support this format with the c flag.
var parsedDate = Ext.Date.parse('1960-05-15T20:00:00', 'c');
var dateStr = Ext.Date.format(parsedDate, 'Y-m-d');
// "1960-05-15"
Have a look at the Sencha ExtJs 6.2.1 documentation Ext.Date for further informations.
You can use native JS to accomplish the output of the Date object in to this format yyyy-mm-dd
Like so:
var val = '1960-05-15T20:00:00';
var d = new Date(val);
var date = d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2);
console.log(date);
When you do
var a = new Date(someDate)
variable a contains date according to your local timezone.
If you want date in same format as you entered , use toISOString method
var a = (new Date(someDate)).toISOString()
My recommendation would be, you can only assume the timezone from where the date is coming from. If you know exactly where that date is coming from, a.k.a London, New York, Sidney, etc... then you can use momentjs to set the UTC offset
var val = "1960-05-15T20:00:00"
// these are equivalent
moment(val).utcOffset("+08:00");
moment(val).utcOffset(8);
moment(val).utcOffset(480);
So the OP has said they're in
Tbilisi, Georgia GMT + 4.00
so
moment(val).utcOffset("+04:00");

Converting string to date object, adding two hours and converting back to string (JavaScript)

I have a date string, want to convert it into a date object, add 2 hours and print out the converted date object back to a variable. But I get the error listed bellow:
// dateTime: 2013-09-27 09:50:05
var dateTime = $("#inputDatetime").val();
var startDate = dateTime;
var date = new Date(startDate);
var duration = 2;
var endDate = date;
endDate.setHours(date.getHours()+duration)
var dateString = endDate.format("dd-m-yy hh:mm:ss");
Error:
Uncaught TypeError: Object [object Date] has no method 'format'
Why do I get this TypeError?
Vaibs,
there is no method "format", you can do formating using available methods from Date Object.
please don't use plugin
example :
// dd-mm-yy hh:mm:ss
function formatDate(date) {
return ((date.getDate()<10?'0':'')+date.getDate()) + "-"+
(((date.getMonth()+1)<10?'0':'') + (date.getMonth()+1)) + "-" +
date.getFullYear() + " " +((date.getHours()<10?'0':'')+date.getHours()) + ":" +
(date.getMinutes()<10?'0':'') + date.getMinutes() + ":" +
(date.getSeconds()<10?'0':'') + date.getSeconds();
}
*thank you #donot
Use jquery ui date parser.
http://docs.jquery.com/UI/Datepicker/parseDate
This is the best function for parsing dates out of strings that I've had the pleasure to work with in js. And as you added the tag jquery it's probably the best solution for you.
.format() is not a valid Date method. JavaScript does not have an easy way to format dates and times with a user-specified format. The best you can do (without separate plugins) is to create your own string by getting each component of the date separately, and formatting/concatenating them.
I've used this before and it seems to work!
var dateString = endDate.toString("dd-m-yy hh:mm:ss");

Categories

Resources