How to convert date format coming in an ajax object? - javascript

I am getting 2018-06-10 00:29:04 this type of value in the key date. I just want to display date without time.
I want to have 2018-06-10 from 2018-06-10 00:29:04.

If it's a fixed string that you're working with, as in you know it'll always be in that format, you can just truncate it like so:
var datetimestamp = "2018-06-10 00:29:04";
var dateTruncated = datetimestamp.slice(0, 10);
If it's not fixed, you can split on spaces like #Shubh mentioned in his comment and take the first array value.

Related

How to combine Date and Time from 2 different var's in NodeJS

I have a User Input which consists of a Date and a Time Input. Both of those values are send as a full date like
reminder_date: '2021-02-15T08:00:00.000Z',
reminder_time: '2021-02-09T17:00:00.000Z'
But i want to store it in my db as one single value like
'2021-02-15T17:00:00.000Z'
what is the best approach to achieve that ? split the strings on T and then combine or is there a simple way i can take the date part and the time part and create a new dateTime to save
If the two items are strings, you could simply use the JavaScript substr function to get the first 11 characters from reminder_date and the second part of reminder_time (starting from 11), then concatenate them, e.g.
let reminder_date = '2021-02-15T08:00:00.000Z';
let reminder_time = '2021-02-09T17:00:00.000Z';
let reminder_date_time = reminder_date.substr(0, 11) + reminder_time.substr(11);
console.log(reminder_date_time);

How to "unformat" a numerical string? JavaScript

So I know how to format a string or integer like 2000 to 2K, but how do I reverse it?
I want to do something like:
var string = "$2K".replace("/* K with 000 and remove $ symbol in front of 2 */");
How do I start? I am not very good regular expressions, but I have been taking some more time out to learn them. If you can help, I certainly appreciate it. Is it possible to do the same thing for M for millions (adding 000000 at the end) or B for billions (adding 000000000 at the end)?
var string = "$2K".replace(/\$(\d+)K/, "$1000");
will give output as
2000
I'm going to take a different approach to this, as the best way to do this is to change your app to not lose the original numeric information. I recognize that this isn't always possible (for example, if you're scraping formatted values...), but it could be useful way to think about it for other users with similar question.
Instead of just storing the numeric values or the display values (and then trying to convert back to the numeric values later on), try to update your app to store both in the same object:
var value = {numeric: 2000, display: '2K'}
console.log(value.numeric); // 2000
console.log(value.display); // 2K
The example here is a bit simplified, but if you pass around your values like this, you don't need to convert back in the first place. It also allows you to have your formatted values change based on locale, currency, or rounding, and you don't lose the precision of your original values.

how to append two strings in javascript without space?

I have two strings one brings me a time ie:
var gettime= $("#select-choice-2 :selected").text();
it gives me time in 24 hr format like this
17:45
but i want my time to be in a format like
17:45:00.000
for which i made a string
var ext=':00.000';
I want these two strings to concatenate in such a way to give me proper result.
I see now whats the problem is my "gettime" is not a proper string, i tried it to show in alertbox but nothing happens, so please tell me how to convert gettime into a string.
I got it "gettime" is a local variable and ext is using in some other function thats why "gettime" was not appearing in alertbox, stupid ehh :p
Simply use the concatenation operator:
alert( gettime + ext );
Do you just wand to add the strings together?
In that case:
var bothstrings = gettime + ext;
some browsers can cause line-breaks to the resulting string if you directly assign a + between strings to joing them. The standard way to do is -
gettime.concat(ext)
**
scope problem,i get it now. gettime was a local variable within some function and ext is held in different function thats why in the function gettime is not appearing in alertbox **

JavaScript Date object not working with string passed to it

I am trying to create a date object by using a variable obtained from a database. The string is already in the correct format, already comma delimited "yyyy,mm,dd,hh,mm,ss". However trying to create a Date object returns an Invalid Date error.
var foo ='2012,03,09,12,00,00,00';
document.write(foo); //<-- obviously writes the string 2012,03,09,12,00,00,00 to the browser
var then=(new Date(foo));
document.write(then); //<-- returns Invalid Date
I have a solution which is the following:
var x = foo.split(/[,]/);
var then = new Date(x[0], x[1], x[2], x[3], x[4], x[5]);
Wondering why this is needed when essentially it's recreating the same string that was passed to it.
It's because the string you are trying to convert into a Date object is not valid. The Date object doesn't just accept any format as a string. if it is not recognised it wont work.
See Date doc https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Here is information about format supported
https://www.rfc-editor.org/rfc/rfc2822#page-14
"yyyy,mm,dd,hh,mm,ss" is not a "correct format" for a date string.
The JavaScript Date object can only parse specific formats. Check the MDN docs for Date for valid dateStrings. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Anyway. your 2nd example works because you're not recreating the string, you are are passing 6 different parameters compared to one long one.
You can't pass a comma-separated string to a function and expect it to break it into parameters, it doesn't work that way.

Date extraction JavaScript for Acrobat does not work

Here's the breakdown:
What code do I need to extract the 12, 26, and 48 from a field whose value is 101219488926 and then display it in the format MM/DD/YY? (So in this case, the new value would need to be 12/26/48)
Here's the long version:
I'm using a mag-stripe reader that takes information from the swiped card (a driver license) and then uses that info to auto-populate certain fields in a PDF (first name, last name, date of birth, etc).
Everything works fine, with one exception: the date of birth. Even that does technically work, but the value is in this format (assuming the person's DOB is 26 December 1948):
101219488926
What I need is: the month (12), day (26), and year (1948) stripped out of that long number, then converted to display in the format MM/DD/YY
Outside of Acrobat, this seems to work just fine:
var dob = 101219488926;
trimmonth = dob.substring(2,4);
trimday = dob.substring(10,12);
trimyear = dob.substring(6,8);
dob.value = trimmonth + "/" + trimday + "/" + trimyear;
Any suggestions?
The code you have there shouldn't work - substring is a String function, so you would need to convert that number you have to a string for it to be available. Setting dob.value is also suspect, since dob is a Number, and numbers do not have a value property.
Of course, it's obvious that you're not showing the actual code you have tried, but something like this would probably work:
// Appending blank string to type coerce
var dob = 101219488926 + '';
// Array.join to glue the numbers together
// (no reason why you **have** to use this; the original method will work fine too)
dopInput.value = [dob.substring(2,4), dob.substring(10,12), dob.substring(6,8)].join('/');

Categories

Resources